[R] setting options only inside functions

Hadley Wickham hadley at rice.edu
Wed Apr 27 20:58:29 CEST 2011

> This has the side effect of ignoring errors
> and even hiding the error messages.  If you
> are concerned about multiple calls to on.exit()
> in one function you could define a new function
> like
>  withOptions <- function(optionList, expr) {
>   oldOpts <- options(optionList)
>   on.exit(options(oldOpts))
>   expr # lazily evaluate
>  }

I wish R had more functions like this.  This sort of behaviour is also
useful when you open connections or change locales.  Ruby's blocks
provide nice syntactic sugar for this idea.


Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University

More information about the R-help mailing list