[R] temporarily modify par values?

Jim Ottaway j.ottaway at lse.ac.uk
Sat Apr 4 15:18:16 CEST 2009


>>>>> Romain Francois <romain.francois at dbmail.com> writes:

> "with" is generic, so you could do something like that:

>> par <- function( ... ) structure( graphics::par( ... ), class = "par" )
>> with.par <- function( data, expr, ... ){
> +    old.par <- data
> +    expr
> +    invisible( par( old.par ) )
> + }
>> with( par( mar = c(0,0,0,0) ), plot( 1:10, 1:10 ) )
>> plot( 1:10, 1:10)

It would be nice to add exception handling to this, to clean up on
error. Something like unwind-protect in Lisp.

Here's a naive implementation:


 with.par <- function(data, expr, ... ){
   olderror <- getOption("error")
   options(error=function () {
     par(old.par)
     options(error=olderror)
   })
   old.par <- data
   expr
   options(error=olderror)
   invisible(par(old.par))
 }

Is this a reasonable way to do such things? [I'm new to this kind of
stuff in R]

Or is there a 'with.error.handler' or 'with.unwind.protection' wrapper?
;-)

Yours sincerely,
-- 
Jim Ottaway




More information about the R-help mailing list