[Rd] 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), ...) {} -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] setting global options for a package
Have you considered the lattice package? The defaults can be accessed/changed via trellis.par.get(), but also passed as arguments into the functions. Kevin On Thu, May 10, 2012 at 8:59 AM, Michael Friendly frien...@yorku.ca 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)) } My function could then use foo - function(x, getOption(heplot.colors), ...) {} -- 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Kevin Wright __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] setting global options for a package
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 StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] setting global options for a package
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 StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] setting global options for a package
On 10/05/2012 1:53 PM, Duncan Temple Lang wrote: 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)), ...) { If each option is only used in a small number of places, that's the easiest solution. If they are used more widely, you have the problem of keeping the defaults consistent. Several packages do their own home-brewed solutions to this. In rgl it's done by having a package global variable r3dDefaults. If a user changes it, they get their own copy in the global environment. This means functions within rgl need to use get(r3dDefaults, envir=.GlobalEnv) to do the lookups in the right place. The igraph package also handles defaults for graph colors; I haven't really looked into how they did it. You can do it using lexical scoping: something like myOptions - local({ opt1 - default1 opt2 - default2 function(...) { # If ... has no names, it's asking for values; if it has names, then change # the parents. Or just create two functions, one for setting, one for getting. } ) Duncan Murdoch 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 StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel