Search code examples
rpngr-markdown

Plot knitting error : "unable to start png() device"


I'm using Rmarkdown to produce beautiful documents (like with LaTex), but there is a problem I can't solve.

I'm printing graph in the following way:

```{r p(s|r)}

pleft=function(x, p=0.5){dnorm(x, mean=35, sd = 10)*p/(dnorm(x, mean=35, sd 
= 10)*p+dnorm(x, mean=65, sd = 10)*(1-p))}
pright=function(x, p=0.5){dnorm(x, mean=65, sd = 10)*(1-p)/(dnorm(x, 
mean=35, sd = 10)*p+dnorm(x, mean=65, sd = 10)*(1-p))}

pleft50= function(x){pleft(x, 0.5)}
pright50=function(x){pright(x, 0.5)}

curve(pleft50, from=-10, to=110, xlab="Firing Rate r (Hz)", ylab="p(s|r)", 
col="red", lwd=2)
curve(pright50, from=-10, to=110, xlab="Firing Rate r (Hz)", ylab="p(s|r)", 
col="blue", lwd=2, add=TRUE)
legend("right", legend = c("p(Left|r)","p(Right|r)"), col=c('red', 'blue'), 
lwd = 2)
title("Posteriors")

```

This has worked in the same way in every precedent code chunk and document, but now it raises this error when I knit the document:

Error in png(..., res = dpi, units = "in") : unable to start png() device Calls: ... in_dir -> plot2dev -> do.call -> -> png In addition: Warning messages: 1: In png(..., res = dpi, units = "in") : unable to open file 'ExSheet4_files/figure-html/name of my chunk-1.png' for writing 2: In png(..., res = dpi, units = "in") : opening device failed

I've tried a anything I know, it raise it as soon as curve(pleft50,... is called.

Thank you for your answer and sorry for my english !


Solution

  • It doesn't like the p(s|r) in the first line -- it's trying to create a file for writing and it's failing there. If you remove it, e.g.:

    ---
    title: "Untitled"
    date: "April 14, 2018"
    output:
      html_document: default
      word_document: default
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r}
    pleft=function(x, p=0.5){dnorm(x, mean=35, sd = 10)*p/(dnorm(x, mean=35, sd 
    = 10)*p+dnorm(x, mean=65, sd = 10)*(1-p))}
    pright=function(x, p=0.5){dnorm(x, mean=65, sd = 10)*(1-p)/(dnorm(x, 
    mean=35, sd = 10)*p+dnorm(x, mean=65, sd = 10)*(1-p))}
    
    pleft50= function(x){pleft(x, 0.5)}
    pright50=function(x){pright(x, 0.5)}
    
    curve(pleft50, from=-10, to=110, xlab="Firing Rate r (Hz)", ylab="p(s|r)", 
    col="red", lwd=2)
    curve(pright50, from=-10, to=110, xlab="Firing Rate r (Hz)", ylab="p(s|r)", 
    col="blue", lwd=2, add=TRUE)
    legend("right", legend = c("p(Left|r)","p(Right|r)"), col=c('red', 'blue'), 
    lwd = 2)
    title("Posteriors")
    ```
    

    You get this:

    test knitr