[R] setting options only inside functions

William Dunlap wdunlap at tibco.com
Wed Apr 27 20:42:20 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Jannis
> Sent: Wednesday, April 27, 2011 11:16 AM
> To: Jonathan Daily
> Cc: r-help at r-project.org
> Subject: Re: [R] setting options only inside functions
> 
> Thanks to all who supplied suggestions. All of them worked. 
> The best solution, however, was to wrap the stuff inside 
> dummy() after the options(...) into a try() command. That way 
> it also worked with the following setup:
> 
> 
> 
> dummy=function()
> {
>   old.options=options(error=quote{dummy1()})
> 
>   try(....,silent=TRUE)
> 
>   options(old.options)
> }
> options(error=quote{dummy()})

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
 }
and use it like
 > withOptions(list(warn=0), { warning("Hmm"); stop("Oops")})
 Error in withOptions(list(warn = 0), { : Oops
 In addition: Warning message:
 In withOptions(list(warn = 0), { : Hmm
 > withOptions(list(warn=1), { warning("Hmm"); stop("Oops")})
 Warning in withOptions(list(warn = 1), { : Hmm
 Error in withOptions(list(warn = 1), { : Oops
 > withOptions(list(warn=2), { warning("Hmm"); stop("Oops")})
 Error in withOptions(list(warn = 2), { : (converted from warning) Hmm
 > withOptions(list(warn=-1), { warning("Hmm"); stop("Oops")})
 Error in withOptions(list(warn = -1), { : Oops
 > getOption("warn") # it started out as 0
 [1] 0
or
 > withOptions(list(width=40), print(1:30))
  [1]  1  2  3  4  5  6  7  8  9 10 11 12
 [13] 13 14 15 16 17 18 19 20 21 22 23 24
 [25] 25 26 27 28 29 30
 > getOption("width")
 [1] 80

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> 
> 
> The suggestion of Uwe did not work with these nested error 
> handling functions. I, however, did not state my problem 
> precisely enough for this.
> 
> 
> Thanks again
> 
> Jannis
> 
> 
> --- Jonathan Daily <biomathjdaily at gmail.com> schrieb am Mi, 27.4.2011:
> 
> > Von: Jonathan Daily <biomathjdaily at gmail.com>
> > Betreff: Re: [R] setting options only inside functions
> > An: "Jannis" <bt_jannis at yahoo.de>
> > CC: r-help at r-project.org
> > Datum: Mittwoch, 27. April, 2011 13:35 Uhr
> > There is probably a more elegant way
> > to do this, but you could write
> > it into dummy1():
> > 
> > dummy1 <- function()
> > {
> > ...original function
> > options(old.options)
> > }
> > 
> > Alternatively, you could use ?tryCatch with the finally
> > argument as a
> > call to options.
> > 
> > HTH,
> > Jon
> > 
> > On Wed, Apr 27, 2011 at 9:16 AM, Jannis <bt_jannis at yahoo.de>
> > wrote:
> > > Dear list members,
> > >
> > >
> > > is it possible to set some options only inside a
> > function so that the original options are restored once the
> > function is finished or aborted due to an error?  Until now
> > I do something like:
> > >
> > >
> > > dummy=function()
> > > {
> > >  old.options=options(error=dummy1())
> > >
> > >  ....
> > >
> > >  options(old.options)
> > > }
> > >
> > >
> > > This works for most cases but when the function
> > terminates because of an error and its last command is not
> > run, error=dummy1() still remains as an option. Is there any
> > way around this?
> > >
> > >
> > > Cheers
> > > Jannis
> > >
> > >
> > > ______________________________________________
> > > R-help at 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.
> > >
> > 
> > 
> > 
> > -- 
> > ===============================================
> > Jon Daily
> > Technician
> > ===============================================
> > #!/usr/bin/env outside
> > # It's great, trust me.
> >
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list