>>>>> Romain Francois <romain.franc...@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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to