5

I am new to R so I dont exactly know what kind of information to provide to get help but here it goes: I get the error "Error in dev.off() : cannot shut down device 1 (the null device)" when I try to make a plot... any plot. My latest code is:

pdf("spec_accum_w_sample.pdf")
plot(specaccum(counts))
dev.off()

An empty pdf file and a plot get successfully produced but when I write dev.off() it does not write into the created pdf. Instead I get the error above. I have tried restarting RStudio as well as anything else suggested on this link: Error in dev.off() : cannot shut down device 1 (the null device). I have also tried the initial code suggested on the ticket to re-create the author's pie chart, but still get the same error despite trying all of the suggestions

I do not find any other place that addresses this specific error. I am running R version 3.4.4 (2018-03-15) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Sierra 10.12.6

Is there anything else I can try? Am I supposed to install something or update something to get dev.off() to work?

Angelina
  • 51
  • 1
  • 1
  • 2
  • You get the same error with `plot(1:10. 1:10)`? If not, where does the `specaccum` function come from? Are you sure there are no other error messages? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 25 '18 at 18:08
  • specaccum is from the vegan package. But it doesnt matter the plot. I get the same dev.off() error any plot including the plot you suggested. I see the plot in my R notebook but I cannot print it into the test.png file i have created for it – Angelina Apr 25 '18 at 18:36
  • Oh, you are using R Notebooks? That's kind of an important detail. Do it work fine if you just run it in the R console? Are you trying to echo the plot into the notebook AND write it to a file? – MrFlick Apr 25 '18 at 18:40
  • I get the error (null device 1) if I try to run any plot in R console. I think essentially the plot is to echo into the notebook and then write into a file with dev.off(). – Angelina Apr 25 '18 at 18:57
  • As far as I know is necessary that at least one device be active, usually, it is not visible. Then when you try to close it with dev.off() you'll get the error. – Irbin B. Aug 30 '18 at 05:08

2 Answers2

3

Try this: (It worked for me but will shut down any plotting device open).

while (!is.null(dev.list()))  dev.off()
Joke O.
  • 515
  • 6
  • 29
  • This is a great suggestion for use in a function that takes a bit longer to output a graphic, i.e. you're looping a facet_grid over a big list. The `dev_off()` kept trying to run in the middle of the grid drawing. – Anonymous coward Jan 30 '20 at 22:35
1

Make sure your pdf is produced in your working directory. Run getwd() to check what's the working directory. If the pdf is not there, change the working directory to where the pdf is produced with setwd("/my/working/directory").

Alternatively try opening a new device and then create a pdf, a plot, and write it into the pdf:

dev.new()
pdf("spec_accum_w_sample.pdf")
plot(specaccum(counts))
graphics.off()
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
  • Thanks for the suggestion. I set my working directory but still the plot dev.off() gave me the same error – Angelina Apr 25 '18 at 20:12
  • Does saving as png throws the same error. To test just use `png()` function instead of `pdf()`. – PiotrWolkowski Apr 25 '18 at 20:27
  • tested png(), jpg(),pdf() and svg() functions. I get the empty file created with the given name in my wd, I get a ready plot echo-ed in my Notebook. I just cant get dev.off() to fill the empty file with the image of the plot – Angelina Apr 25 '18 at 20:33
  • Hacky but can you try to shut down all open devices? Use `graphics.off()` instead of `dev.off()`. – PiotrWolkowski Apr 25 '18 at 20:40
  • Ok with graphics.off() I dont get an error but my exercise plot plot(1:10,1:10) still does not fill the png file I've created for it. Also is it weird that I dont have jpg() nor png() functions (just svg() and pdf() have thus far created empty files successfully) – Angelina Apr 25 '18 at 21:33
  • I edited my answer and added code to open a new device first. Can you try this? – PiotrWolkowski Apr 26 '18 at 12:10
  • Thanks, I just tried. I get the same result = no plot in my test.pdf. No error tho – Angelina Apr 26 '18 at 16:42
  • 1
    This is late to the party, but notebooks are different from `.R` files. When using notebooks RStudio seems to divert graphics in a way that is different from plain code files. We tried all of the fixes we found online and none of them worked. Then we moved the offending code to a clean file named `test.R` and everything worked perfectly. The context of executing from a notebook file seems to be the cause as those are not built for exporting graphics easily, at least not with the set device/plot/close device pattern. – Doctorambient Apr 05 '19 at 15:52