Skip to content
Prev 61935 / 63421 Next

Problems caused by dev.off() behaviour

On 02/10/2023 10:17 p.m., Trevor Davis wrote:
Right, I see.  With some more fiddling, I've decided that I don't like 
the error you get if you try to close device 1, so here's the current 
version:

safe.dev.off  <- function(which = dev.cur(), prev = dev.prev()) {
   if (which != 1) {
     force(prev)
     grDevices::dev.off(which)
   }
   if (length(dev.list()))
     dev.set(prev)
   else
     c("null device" = 1)
}

This does the dev.set even if there's only one device so it can return 
the resulting device number.

Duncan Murdoch