[R] setting options in a list

Michael Toews mwtoews at sfu.ca
Mon Oct 23 07:08:12 CEST 2006


Hi,
I have a question regarding setting options that use a list. There are 
none that I'm aware of in R-base, however, it can make sense for some 
custom situations. For example:
 options(myoptions=list(lwd=1,density=NULL,angle=45,col="green"))
Getting values is simple:
 getOption("myoptions")$col
However, setting a value from the list is less intuitive; here are a few 
failed attempts that could make sense from an intuitive level:
 setOption("myoptions")$col <- "red"  # non-existing function
 options("myoptions")[[1]]$col <- "red"
Here is an attempt that almost works, but as documented, it only sets a 
local .Options object for S-compatibility:
 .Options["myoptions"][[1]]$col <- "red"
 .Options["myoptions"][[1]]$col   # works, but ...
 getOption("myoptions")$col

The only method I know of is to modify a copy of the list from the 
options, then re-set the option:
 cp <- getOption("myoptions")
 cp$col <- "red"
 options("myoptions"=cp)
 getOption("myoptions")$col

So, my question is if there is a more elegant method of setting an 
option in a list, that doesn't need copying and multiple commands? 
Having a 'setOption' function sure could be helpful for this instance, 
however I'm unsure how to implement this method (or if it is possible in 
the current version of R).
Thanks.
+mt



More information about the R-help mailing list