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

3

6

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 !

Rosenblum answered 14/4, 2018 at 13:45 Comment(2)
Do you by any chance have a pdf opened with another program?Omophagia
No, only Rstudio for this and ChromeRosenblum
S
4

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

Sandpit answered 14/4, 2018 at 15:9 Comment(5)
Great! If you could check-mark the answer that would be greatly appreciated :)Sandpit
Just to understand : why did this failed here but it worked in a previous chunk with another title (e.g "p(r)")?Rosenblum
I think it's the | but would have to do more testing to know for sure.Sandpit
Done ! Thank you.Rosenblum
Nice catch. I was looking at the chunk name and found it curious but didn't pick up on it because failing to write to a device for me usually meant I had file opened in a program which locks the file.Omophagia
F
1

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.

Frugivorous answered 26/5, 2020 at 12:17 Comment(0)
E
0

I had a similar problem, where one of my code chunks had a "/" character in the middle of it (i.e. "Temperatures for Phoenix / Flagstaff) and it would run, but not knit. But this allowed me to do some research.

When knitting, RStudio creates a folder of all the temp files used to knit, within which is a folder of all your png images. Each image is named the same as the name on the code chunk it comes from, with an extra dash and number to indicate its position in that chunk. On a successful knit, this folder is deleted (but on an unsuccessful one it is not - hence how I learned all this).

For example: an unnamed chunk with two png images would create 2 files in this temp folder named "unnamed-chunk-1-1" and "unnamed-chunk-1-2".

So if any of your code chunks have a name that gives Windows a problem when looking up this temp image to add to your knit file, you get this error. As others have already said, fixing this problem is as simple as making each code chunk have a name that would be a valid filepath (or keep the code chunk unnamed).

Eczema answered 22/7, 2023 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.