Suppressing "null device" output with R in batch mode Suppressing "null device" output with R in batch mode bash bash

Suppressing "null device" output with R in batch mode


For no good reason I'm aware of, dev.off(), unlike device related functions like png() returns a value: "the number and name of the new active device." That value is what's being echoed to stdout.

Suppressing it can thus be achieved by just putting it somewhere, i.e.,

garbage <- dev.off()


One of the nice things about R is that you can view the source of many functions:

> dev.offfunction (which = dev.cur()) {    if (which == 1)         stop("cannot shut down device 1 (the null device)")    .Internal(dev.off(as.integer(which)))    dev.cur()}<environment: namespace:grDevices>

So it calls .Internal(dev.off(...)) and then returns dev.cur(), which I suppose would be useful if you have several devices open so you know which one became active. You could use .Internal(dev.off(as.integer(dev.cur()))) in your script, or even patch dev.off to only return the value of dev.cur() if it is something else than the null device, and send the patch to the maintainers of R.

Also, graphics.off() calls dev.off() for all devices and doesn't return anything.


Ran into the same issue recently and noticed that one more possibility is not mentioned in the answers here:

invisible(dev.off())

This will hide the output from dev.off() and will not create additional variables unlike assigning the output to garbage variable: garbage <- def.off() would.