Skip to content
Prev 43291 / 63421 Next

setting global options for a package

This may be elementary, but I can't find an answer: How can I set up 
global options for
some specific arguments to functions in a package which can be easily 
changed by the user?

This question relates to the selection of colors used in functions in 
several packages (heplots,
genridge), where I want to provide reasonable default values for plots, 
but allow users to
change those defaults globally for all plots produced with my functions.

One solution is to use palette() for the default, as in

foo <- function(x, col=palette(), ...)  {}
but the standard palette is not appropriate for my use, and I'd rather 
not hijack more typical uses

Another is to use an explicit list of colors for default, as in

bar <- function(x, col=c('red', 'blue', 'brown', 'darkgreen', ...), ...)  {}
but this must be overridden each time by someone to wants to change the 
defaults.

options() seems like the way to go, but I'm not sure how to implement 
this.  If I use
a .onLoad function to set some options, will these be created in the 
global environment?
If not, how to make them so?

.onLoad <- function() {
   options(heplot.colors =
   c("red", "blue", "black", "darkgreen", "darkcyan","magenta", 
"brown","darkgray"))
}

My function could then use

foo <- function(x, getOption("heplot.colors"), ...)  {}