6
votes

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 !

2
Do you by any chance have a pdf opened with another program?Roman Luštrik
No, only Rstudio for this and ChromeDrAlfredHawking

2 Answers

4
votes

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

1
votes

The described error occurs, when you are knitting plots within named code chunks, and the name of the chunk does not lead to a valid path name during knitting process.

That is, during the knitting process the plots are written into a temporary path which contains the name of the code chunk, ergo this name should contain only characters that are valid for path names, which was not true for the used character |. One should avoid to use white characters for chunk names as well.