[R-pkg-devel] Error in CHECK caused by dev.off()

Sebastian Meyer @eb@meyer @end|ng |rom |@u@de
Thu Jul 23 16:52:25 CEST 2020


Back to the original topic: graphics.off() is probably not what you
want. It shuts down *all* open graphics devices, not just the current
one. Example code or your plotting functions should not do that.

Calling graphics.off() in example code will also disturb standard R CMD
check. Before running the examples, R CMD check opens a pdf device to
store any graphics output [1]. You will find the resulting pdf file in
{PACKAGE}.Rcheck/{PACKAGE}-Ex.pdf after R CMD check. [BTW, the plots
therein will be usefully annotated with the names of the originating
help pages.]

R CMD check will eventually fail from trying to close this pdf device
after running the examples [2], if you have already closed all graphics
devices (including this pdf device) through code in your examples. This
is where the

Error in grDevices::dev.off() :
  cannot shut down device 1 (the null device)
Execution halted

actually came from.

Finally, many of the graphics devices are platform-specific and the png
device may not even be available. So it is reasonable to condition on
capabilities("png") or to put such examples in \donttest. The latter is
also used in the example code for grDevices::png, at least in the Unix
version of the man page [3].

HTH!

	Sebastian


[1]: file.path(R.home("share"), "R", "examples-header.R")
[2]: file.path(R.home("share"), "R", "examples-footer.R")
[3]:
https://github.com/r-devel/r-svn/blob/15253534aa1f4e91d33d9b0e3f035daedfe750bb/src/library/grDevices/man/unix/png.Rd#L249-L259

BTW, on Unix-alikes, example(png) writes to files myplot.png,
myplot1.jpeg, and myplot2.jpeg in the current working directory. This
should be fixed.



Am 22.07.20 um 19:25 schrieb Helmut Schütz:
> Hi Serguei,
> 
> Serguei Sokol wrote on 2020-07-22 15:51:
>> Hmm... I see 2 possibilities for still getting an error while the
>> concerned part of code is not supposed to be run:
>>
>>  - either you are running not updated version of your package;
> 
> I _can_ built the package and it runs as intended. Only the CHECK throws
> the error.
> 
>>  - or the error comes from some other place of the code.
> 
> Closing the device is required only _once_ in the entire package.
> In my NAMESPACE I have (and had in all previous versions):
> importFrom(grDevices, png, graphics.off, dev.list, dev.off)
> 
>> Sorry but without a minimal reproducible example I cannot help more.
> 
> The problem is that I cannot reproduce it as well. Only CHECK laments
> about dev.off() which I changed to graphics.off() in the meantime.
> 
> library(grDevices)
> foo <- TRUE   # shall we plot?
> png.path <- "~/" # user's home folder
> png.path <- normalizePath(png.path)
> if (foo) {
>   png(paste0(png.path, "test.png"), width = 480, height = 480, pointsize
> = 12)
> }
> plot(x = 0:1, y = 0:1, type = "l", xlab = "x", ylab = "y")
> if (foo) {
>   graphics.off()
> }
> 
> Best,
> Helmut
>



More information about the R-package-devel mailing list