[R] Are there better ways to save and restore par() settings

David Winsemius dwinsemius at comcast.net
Sat May 15 00:19:32 CEST 2010

On May 14, 2010, at 5:45 PM, Duncan Murdoch wrote:

> On 14/05/2010 3:51 PM, David Winsemius wrote:
>> Some of the help page examples use the form:
>> opar <- par(<something>)
>>     .....plotting activities...
>> par(opar)
>> This seems to "work" well, yet I have read in some places
> Please include commented, retrievalbe citations for "some places".

In the examples from help(par) I see:
p <- par(mfrow = c(2, 2),
# 2 x 2 pictures on one plot pty = "s")
  # square plotting region,
  # independent of device size
  ## At end of plotting, reset to previous settings:
# Alternatively, op <- par(no.readonly = TRUE)
# the whole list of settable par's.
## do lots of plotting and par(.) calls, then reset: par(op)
## Note this is not in general good practice
Was it only with the use of the "no.read.only" parameter that one is  
supposed to avoid the form I illustrated. I had assumed it applied to  
both alternatives.
>> that it is  not the preferred method to keep you parameters from  
>> getting  corrupted. What is the preferred method?
> The above, with the restore wrapped in on.exit(), as your sources  
> should have said.

Perhaps it was implied in a manner to which I was not properly  
receptive. The only place I see on.exit() on the par help page is in  
the final example. I do not see a link to that function in the that  
help page, and I had read it but assumed it was related to "exit"-ing  
an R session. On visiting the on.exit help page for the first time in  
my life, I see that its primary use is with graphics device calls. So  
we are being advised to write that wrapper function each time we might  
be making a par-changing plot?


> Duncan Murdoch
>> A worked example from a recent posting with minor additions:
>>   opar <- par(mar=c(3,5,0,5))
>>        layout(rbind(c(4,1,1,5), c(2,2,3,3)), widths=c(1,1,1,1),
>>                           heights=c(.75,2.25))
>>           barplot(0,0, axes=FALSE)
>>             legend(x=0.5, y=0, legend=c("Example 1", "Example 2"),
>>                   pch=c(1,2), cex=1.5, xjust=1, yjust=0.5, bty="n")
>>         plot(1:10,1:10, pch=1)
>>         plot(1:20,1:20, pch=2)
>>  par(opar)

David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list