[R] Official way to set/retrieve options in packages?

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jun 1 23:57:38 CEST 2013


On 01/06/2013 22:44, Anthony Damico wrote:
> hope this helps..  :)
>
>      # define an object `x`
>      x <- list( "any value here" , 10 )
>
>      # set `myoption` to that object
>      options( "myoption" = x )
>
>      # retrieve it later (perhaps within a function elsewhere in the package)
>      ( y <- getOption( myoption ) )
>
>
> it's nice to name your options `mypackage.myoption` so users know what
> package the option is associated with in case they type `options()`
>
>
> here's the `.onLoad` function in the R survey package.  notice how the
> options are only set *if* they don't already exist--

But a nicer convention is that used by most packages in R itself: if the 
option is not set, the function using it assumes a suitable default. 
That would make sense for all the FALSE defaults below.

Note though that this is not 'persistent': users have to set options in 
their startup files (see ?Startup).   There is no official location to 
store package configurations.  Users generally dislike software saving 
settings in their own file space so it seems very much preferable to use 
the standard R mechanisms (.Rprofile etc).

>
>> survey:::.onLoad
>
> function (...)
> {
>      if (is.null(getOption("survey.lonely.psu")))
> options(survey.lonely.psu = "fail")
>      if (is.null(getOption("survey.ultimate.cluster")))
> options(survey.ultimate.cluster = FALSE)
>      if (is.null(getOption("survey.want.obsolete")))
> options(survey.want.obsolete = FALSE)
>      if (is.null(getOption("survey.adjust.domain.lonely")))
> options(survey.adjust.domain.lonely = FALSE)
>      if (is.null(getOption("survey.drop.replicates")))
> options(survey.drop.replicates = TRUE)
>      if (is.null(getOption("survey.multicore")))
> options(survey.multicore = FALSE)
>      if (is.null(getOption("survey.replicates.mse")))
> options(survey.replicates.mse = FALSE)
> }
> <environment: namespace:survey>
>
>
>
>
> On Sat, Jun 1, 2013 at 4:01 PM, Jonathan Greenberg <jgrn at illinois.edu>wrote:
>
>> R-helpers:
>>
>> Say I'm developing a package that has a set of user-definable options that
>> I would like to be persistent across R-invocations (they are saved
>> someplace).  Of course, I can create a little text file to be written/read,
>> but I was wondering if there is an "officially sanctioned" way to do this?
>>   I see there is an options() and getOptions() function, but I'm unclear how
>> I would use this in my own package to create/save new options for my
>> particular package.  Cheers!
>>
>> --j
>>
>> --
>> Jonathan A. Greenberg, PhD
>> Assistant Professor
>> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
>> Department of Geography and Geographic Information Science
>> University of Illinois at Urbana-Champaign
>> 607 South Mathews Avenue, MC 150
>> Urbana, IL 61801
>> Phone: 217-300-1924
>> http://www.geog.illinois.edu/~jgrn/
>> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list