[Rd] Misuse of get(getOption("device"))() in packages

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Apr 26 09:17:08 CEST 2008

Quite a few packages have used this construct to launch a device, but it 
has several flaws.  It's not clear in most cases that a package really 
needs to launch a new device (R will do so if needed), but 2.7.0 provides 
a function dev.new() to do so.  (If you really need this in a GPL-ed 
package that must run in R < 2.7.0, consider copying dev.new.)

You cannot assume any arguments for the default device, and lines like


may throw an error or give you an 8x8 pixel device.  Even worse is

   get(getOption("device"))(8, 8)

as for e.g. X11() and quartz() the first argument is not a dimension, and


applies to just one device (and this user at least does not want to get 
his magnifying glass out to examine plots forced into 6% of his screen 

It is better to adapt your plots to the user's environment than to attempt 
to adapt his/her environment to your plots.  (dev.size(), also new in 
2.7.0, may help you do so.)

The more important flaws are:

1) As from R 2.5.0, options("device") can be a character string or a 
function.  If the latter, this construct does not work.

2) ?options carefully describes the search path for the device if 
specified by name.  get() in a package will not do the same thing, and is 
more likely to pick up another object of the same name.

3) What most of these packages do is to open another device when one is 
already open.  If there is no screen device, this can be disastrous as 
e.g. there may already be a pdf() device open that is writing to 
Rplots.pdf, and  get(getOption("device))() will start another pdf() device 
writing to Rplots.pdf: the result is almost certainly a corrupt PDF file.

The intention is that in R 2.8.0 the out-of-the-box default devices will 
all be functions, so point 1 will apply.  (dev.new will be more 
sophisticated there.)  Many of the packages concerned are now showing 
ERROR in the 'r-devel Linux x86_64' section of


Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list