19
votes

I am using Windows. When trying to plot a graph on png device, it fails with the error.

My code:

png("C:\\plot1.png", width = 480, height = 480, units = "px", bg = "white")

par(mar= c(4, 4, 2, 1))

hist(pwrcon$Global_active_power,col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)")

dev.off()

Error:

Error in png("C:\\plot1.png", width = 480, height = 480, units = "px",  : 
  unable to start png() device
In addition: Warning messages:
1: In png("C:\\plot1.png", width = 480, height = 480, units = "px",  :
  unable to open file 'C:\plot1.png' for writing
2: In png("C:\\plot1.png", width = 480, height = 480, units = "px",  :
  opening device failed

Can anyone help me in getting this resolved?

Thanks in advance

16
@R.Schifini, did you add additional backslashes when editing? If so (and if your diagnosis below is correct), you have made this question very confusing ...Ben Bolker
@BenBolker, I only formatted the code. I agree that it is confusing, but I recall only seeing one backslash. It seems that the problem was solved by reinstalling. I would vote for the whole question to be removed since it doesn't seem to be of further use. (edit: I even copied the original path and filename)R. Schifini
OK, voting to close.Ben Bolker
Yes, most likely this error is caused by a wrong or non-exist folder directory in the filename that we try to write the png. The folder we plan to use should be created in advance (using e.g. if(!dir.exist(folder_name) dir.create(folder_name)).Yang Liu

16 Answers

12
votes

I cannot explain why, but I once found that when the folder path in which my RStudio project was saved was a very long character string, the png device would fail. When I shortened the folder path it worked.

7
votes

I had the same issue while working in an r-markdown document.

The issue in my case had something to do with viewing the Chunk Output Inline. When I switched to viewing the Chunk Output in Console, it worked just fine.

5
votes

Had the same problem on a PC. The problem was that there was an antivirus program with "Safe files" enabled, which blocked Rstudio from creating graphics files. The antivirus didn't display any information when blocking, so it doesn't give you any clues really.

2
votes

The filename C:\plot1.png contains a backslash (\) which is an escape character . This causes the error you are getting.

Change it to a slash (/)

png("C:/plot1.png", width = 480, height = 480, units = "px", bg = "white")

Or double the backslash (\\):

png("C:\\plot1.png", width = 480, height = 480, units = "px", bg = "white")
2
votes

I got also this error: "Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : unable to start png() device"

The name of the .Rmd file that I've been working on was containing some nonenglish characters, so removing them has been helpful in my case.

1
votes

I had the same error message. Turns out there was a typo in the path name. Besides these problems, reinstalling ggplot and tydiverse from CRAN seemed to have worked for some, see here

1
votes

Also met the similar issue in Windows 10, my R script is put in the same folder as RScript.exe, using the package ggplot2. However I got the message could not open file 'Rplot001.png'.

Finally found two ways to solve the problem:

  1. Move the R script to any other folder except the folder RScript.exe located.
  2. Set work directory using the command setwd("YourPath") first, then do other things.
0
votes

One other problem could be that your Rstudio may have updated. I have encountered this problem while working inside R-markdown. Trying the code in a regular R script still works. Try saving the markdown as a new file. This should fix the problem temporarily.

Do not know of a long term solution.

0
votes

I also had this error today when working in an RMarkdown notebook (it was fine yesterday). If I edit a chunk then try to run it I get this error. If I then save the notebook and try the chunk again it works. My working directory is a OneDrive folder. I wonder whether that may be an issue.

Having to save every after edit is not ideal, but a workaround.

0
votes

I added "dev.off() "before plot, the problem solved. The reason may be because of the previous device has not turn off yet.

0
votes

I received the error mentioned above

Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : unable to start png() device Calls: <Anonymous> ...

My issue was with the fig.width option in one of my R-code chunks of my R-markdown document and when the output was rendered as an html document. The fig.width was too large compared to the other fig.width options in other code-chunks. Again this was observed only when I attempted to render it as an html document, not as a powerpoint presentation.

0
votes

This has been solved, but I thought I might add my answer if it makes someone's life easier. Of course you can have your wd set to some short path (or path with no special characters): setwd("c/Users/John/My_r_project) But I use R at work, thus my R project is saved on a common drive with super long path, and my working directory has to be long. A workaround was:

```{r setup, include=FALSE}
knitr::opts_chunk$set(
    fig.path = "c/Users/John/My_r_project/figures" #make sure you create the folder first in Windows
)
```

And you can of course add other options there as well, such as: dpi = 300, echo = FALSE, ...

0
votes

I had the same problem while running R in Jupyter notebook. I did a lot of Google searches and tried everything possible. The only way it worked for me was by restarting the kernel. But, restarting the kernel is not a good solution if you already have trained your models which took you a long time.

0
votes

This started happening to my while using RMarkdown. My solution is to restart R and clear the output, and re-run. I don´t know why it happens.

0
votes

I once also run into this problem. For me the first solution worked, but you might also want to check the other two options.

  1. Restart R session. Tab Session > Restart R (Ctrl + Shift + F10)
  2. Check working directory using getwd() and change it is necessary using setwd(path)
  3. Don't forget to close the device using dev.off()
-1
votes

I have seen this message, and find this is because the png file is occupied by another software or process.

So close the software or process, then restart rstudio.