Re: [Rd] setting global options for a package

From: Duncan Temple Lang <duncan_at_wald.ucdavis.edu>
Date: Thu, 10 May 2012 10:53:25 -0700

Or slightly more conveniently, use the default value of getOption() to return the vector of color names if the option is not set, e.g.

 foo <- function(x, heplot.colors = getOption("heplot.colors",

                                               c("red", "blue", "black", "darkgreen", "brown", "darkgray")), ...)  {


   D.

On 5/10/12 10:09 AM, Simon Urbanek wrote:

> 
> On May 10, 2012, at 9:59 AM, Michael Friendly wrote:
> 

>> 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"))
> 
> You certainly don't want to do that - it would override user's setting and thus defeat the whole purpose of options.
> 
> 

>> }
>>
>> My function could then use
>>
>> foo <- function(x, getOption("heplot.colors"), ...) {}
>>
> 
> You can always do that:
> 
> foo <- function(x, heplot.colors = getOption("heplot.colors"), ...)  {
>   if (is.null(heplot.colors)) heplot.colors <- c("red", "blue", "black", "darkgreen", "darkcyan","magenta", "brown","darkgray")
> 
> Cheers,
> Simon
> 
> 

>>
>> --
>> Michael Friendly Email: friendly AT yorku DOT ca
>> Professor, Psychology Dept.
>> York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
>> 4700 Keele Street Web: http://www.datavis.ca
>> Toronto, ONT M3J 1P3 CANADA
>>
>> ______________________________________________
>> R-devel_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
> 
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 10 May 2012 - 17:55:40 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 May 2012 - 11:31:04 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive