Error : Unable to start png() device

Viewed 59724

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

18 Answers

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.

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.

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.

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.

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.

I came across the same type of error, neither png nor jpg device could not open from ggplot2's ggsave command. The path contained an Å symbol which was stored as \305 when inserted in a variable describing the directory.

setwd(path) followed by ggsave(filename)for this directory variable did work in this case, but ggsave(paste(path, filename, sep="/") did not.

Replacing the Å with an A in my case could resolve the error.

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

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.

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.

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.

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

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, ...

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.

If you are trying to save the PNG image, make sure you have permission to create files in the destination folder. I've seen this error come up on a corporate network where full access had not been granted to the user.

The error usually means that the file cannot over accessed or overwritten, meaning that the file is in use or the file path is not writeable.

In the first case, just close the files you have open (e.g., in an image preview) and try again.

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.

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()
Related