setting option in function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

setting option in function

Charles Geyer
is it possible to set an option inside a function ((I want to set
na.action = na.fail) and have the previous state restored if there is
an error so that the function doesn't change the option behind the
user's back?

Sorry if this has been answered before, but this subject is hard to Google.

--
Charles Geyer
Professor, School of Statistics
University of Minnesota
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: setting option in function

Thomas Lumley-2
old_options <- options(na.action=na.fail)
on.exit(options(old_options))

You can also use this to define a wrapper that executes an expression
using special options

withOptions<-function(optlist,expr){
        oldopt<-options(optlist)
        on.exit(options(oldopt))
        expr<-substitute(expr)
        eval.parent(expr)
        }


    -thomas



On Sat, Oct 20, 2012 at 10:35 AM, Charles Geyer <[hidden email]> wrote:

> is it possible to set an option inside a function ((I want to set
> na.action = na.fail) and have the previous state restored if there is
> an error so that the function doesn't change the option behind the
> user's back?
>
> Sorry if this has been answered before, but this subject is hard to Google.
>
> --
> Charles Geyer
> Professor, School of Statistics
> University of Minnesota
> [hidden email]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



--
Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: setting option in function

Charles Geyer
That was easy.  Thanks.  That will fix a problem a lot of users are having
with the aster package.  If users have NA's in their data, then they almost
certainly don't know what they are doing (with aster).

On Fri, Oct 19, 2012 at 5:10 PM, Thomas Lumley <[hidden email]> wrote:

> old_options <- options(na.action=na.fail)
> on.exit(options(old_options))
>
> You can also use this to define a wrapper that executes an expression
> using special options
>
> withOptions<-function(optlist,expr){
>         oldopt<-options(optlist)
>         on.exit(options(oldopt))
>         expr<-substitute(expr)
>         eval.parent(expr)
>         }
>
>
>     -thomas
>
>
>
> On Sat, Oct 20, 2012 at 10:35 AM, Charles Geyer <[hidden email]>
> wrote:
> > is it possible to set an option inside a function ((I want to set
> > na.action = na.fail) and have the previous state restored if there is
> > an error so that the function doesn't change the option behind the
> > user's back?
> >
> > Sorry if this has been answered before, but this subject is hard to
> Google.
> >
> > --
> > Charles Geyer
> > Professor, School of Statistics
> > University of Minnesota
> > [hidden email]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>
> --
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
>



--
Charles Geyer
Professor, School of Statistics
University of Minnesota
[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: setting option in function

William Dunlap
In reply to this post by Charles Geyer
Hi Charley,

You can use the idiom
   oldNaAction <- options(na.action=na.fail)
   on.exit(options(oldNaAction))
or the slightly safer (protected again an interrupt
between the first two lines or an error calling options
to set them)
   oldNaAction <- getOption("na.action")
   on.exit(options(oldNaAction))
   options(na.action=na.fail)
.
I prefer to bury it in a function like
   withOption <- function(optionList, expr) {
       stopifnot(is.list(optionList))
       oldOptionList <- options(names(optionList))
       on.exit(options(oldOptionList))
       options(optionList)
       expr # lazily evaluate now
   }
used as
  > getOption("na.action")
  [1] "na.omit"
  > withOption(list(na.action = "na.fail"), lm(y~x, data=data.frame(x=c(1,2,NA), y=1:3)))
  Error in na.fail.default(list(y = 1:3, x = c(1, 2, NA))) :
    missing values in object
  > withOption(list(na.action = "na.exclude"), lm(y~x, data=data.frame(x=c(1,2,NA), y=1:3)))  

  Call:
  lm(formula = y ~ x, data = data.frame(x = c(1, 2, NA), y = 1:3))
 
  Coefficients:
  (Intercept)            x  
            0            1  

  > getOption("na.action")
  [1] "na.omit"

The function makes it  less painful to use the safer version and you
don't have to worry about a subsequent call to on.exit()
wiping out the current request to do something on exiting
the function.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Charles Geyer
> Sent: Friday, October 19, 2012 2:35 PM
> To: [hidden email]
> Subject: [Rd] setting option in function
>
> is it possible to set an option inside a function ((I want to set
> na.action = na.fail) and have the previous state restored if there is
> an error so that the function doesn't change the option behind the
> user's back?
>
> Sorry if this has been answered before, but this subject is hard to Google.
>
> --
> Charles Geyer
> Professor, School of Statistics
> University of Minnesota
> [hidden email]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel