[Rd] Multiple options for a package

Eric Lecoutre lecoutre at stat.ucl.ac.be
Tue Dec 14 11:52:16 CET 2004



Hi R-devel,

I am facing a situation where the number of options I would like to propose 
to the user is somewhat big (and could easily increase more and more as I 
will code up a little more - even coming to a point where an user should be 
able to implement his own options).

What we have to handle options is the couple:
options(par=value) and getOption("par")

I was aking myselft what would be the "better" strategy to handle a bunch 
of options for a package.

I ended up with the idea of storing a list, as my options would also be 
classified, with something like:

--
MyPkgOptions = 
list(set1=list(par1=1,par2=2),set2=list(subset1=list(par1=11,par2=22),subset2=list(par1=111,par2=222)))
options(PkgName=MyPkgOptions)
--

Then, to make easier the access to an element, I tweaked a little bit 
getOption, with the following version:

--
getOption <- function(x,...)
{
   op = options(x)[[1]]
   if (length(list(...))>0) op <- op[[c(...)]]
   return(op)
}
--

Making possible calls like:

---
getOption("PkgName","set2","subset2","par1")
[1] 111
---

Then, I began to implement things 
like  SetPkgOption(pkg,value=NULL,pathToValue) and 
getPkgOption(pkg,pathToValue)

But I wonder if this wont be easier / more efficient at the C level. Sorry: 
I dont propose myself to make it, as my C skills are nearly null.



To recap:

- I need a way to set/get a lot of options for a package
- I am ready to go on with my appraoch, delivering at the end some R functions
- Seeing the way options() are handled with the internal call, I wonder if 
my idea is the better one
- Specifically, I think someone with greater C skills should be able to set 
up functions like PkgOptions
- I would like to hear about any other idea that would better suit me needs

Best wishes,

Eric


PS: I think handling options at a package level would be a benefit for the 
user. Setting options would be done within .First or .onLoad when we know 
the package name. The options() tree would be far more readable, separating 
core options from others. Two weeks ago, i ended up with a list of 125 
elements...


PS2: an other related topic is Saving/Restore options. For my personal 
needs (testing within a session), I coded following functions:


saveOptions <- function(file="R.options",...){
   opts=options(...)
   save(opts,file=file)
}

restoreOptions <- function(file="R.options"){
   bool=TRUE
   .tmp=new.env()
   if (!file.exists(file))
     {
     warning(paste("file ", file, " does not exist"))
     bool=FALSE
     }
   else
   {
   }
   load(file,.tmp)
   opts = get("opts",envir=.tmp)
   options(opts)
   return(bool)
}

Same scheme could be used for a set of options (say options for a package). 
Any comment on the above code?



More information about the R-devel mailing list