Skip to content
Back to formatted view

Raw Message

Message-ID: <8763hkblrb.fsf@lse.ac.uk>
Date: 2009-04-04T13:18:16Z
From: Jim Ottaway
Subject: temporarily modify par values?

>>>>> 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