Error in grDevices::png

Hi all,
Some of my students are running into errors with ggsave() that I haven't come across before. I did find a single post on here from a couple of months ago but it is not exactly similar to what is being reported to me and, in any event, there was not a resolution posted.

In both .Rmd notebooks and in the console, I've had two reports of this error:

> ggsave("Results/histogram.png")
Error in grDevices::png(..., res = dpi, units = "in") : unable to start png() device`

In one case the student was using RStudio version 1.1.414 and in the other case they were using 1.1.419. In both cases they have Kite-Eating Tree installed and have the CRAN version of ggplot2 installed as well. They student running the slightly older version of RStudio reported that the error stopped cropping up after they updated RStudio.

If anyone has ideas about how to troubleshoot this I'd love to know! I don't have a reprex from either student (we're going over it next week in class...wishing we had got to it this past week). Thanks for your help!

Chris

2 Likes

A couple of google searches suggest this might be related to size?

https://groups.google.com/forum/#!topic/ggplot2/YUDRojjWWgo

Thanks @jrlewi - I saw this but thought initially that it might not apply here. My thinking is that if this were the case, it should happen to more than a few of my students (my Mac has no problem creating these plots, nor does my Windows virtual machine on my work iMac). They are also not manually setting sizes here, just using the default 7 x 7 output. Is it possible my logic on this is flawed?

True - and you don't appear to be seeing the exact warning messages as those posts. As a test - I'd try saving as a pdf or some other format. It is possible their systems are not recognizing how to call the png device -- I am not an expert on exactly how this works, but I believe I have seen situations like this before.

Ah this is a great suggestion @jrlewi thanks! I'll see if I can get a student to try this out...

Good luck - let me know how it goes...I am curious.

Hey @jrlewi and anyone else interested - I still haven't been able to figure this out. It seems like in some cases it could be related to students' workflows and that the actual error message is a red herring.

In other cases, however, they are able to write using the pdf device, so the issue doesn't seem to be related to their process. At least one student has re-installed ggplot2 from CRAN on its own, and not with tidyverse, and that seems to have addressed the issue.

1 Like

I have a code that I run to generate 8-10 plots, out of which 2 plots don't get saved and the above error presents itself. I am able to generate the rest of the plots. The complete error and warning message is:
Error in grDevices::jpeg(..., res = dpi, units = "in") :
unable to start jpeg() device
In addition: Warning messages:
1: In grDevices::jpeg(..., res = dpi, units = "in") :
unable to open file 'C~/BRNS Output Files/Trial84/Concs_time_ltsbiomassp1.jpg' for writing
2: In grDevices::jpeg(..., res = dpi, units = "in") :
opening device failed

Have you tried what Chris mentioned above?

1 Like

I'll add that, while I never directly confirmed this, I suspect at least some of my students last semester were trying to write to directories that did not exist. For example:

> library(ggplot2)
> ggplot(data = mpg) +
     + geom_histogram(mapping = aes(hwy))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
> ggsave(file = "img/test.png", dpi = 300)
Saving 7.75 x 7.94 in image
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Error in grDevices::dev.off() : 
  QuartzBitmap_Output - unable to open file 'img/test.png'

The directory img/ does not exist in my working directory in this example. The output makes it seem like something is wrong with the image driver but really it is a file path issue.

5 Likes

Yes, I downloaded ggplot2 from CRAN, and it did not address the issue. I am still trying figure out of the installation actually worked in the first place! I will be grateful if I could get help to check how to confirm this.

This has worked! I should have inspected the file paths more closely.
Thank you for the tip.

If you run library(ggplot2) and it loads, then you've installed it successfully (to see which version, you can run sessionInfo() (with the package loaded), or packageVersion("ggplot2").

Thank you for sharing, Mara. This is useful information.

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

1 Like

Since I posted the original question, I'll follow-up here and then marked it answered. These grDevices errors are often a reference to errors with the specified file path!

Double check your file path refers to a director that exists relative to your current working directory.

6 Likes