Skip to content

[R-pkg-devel] Recommendations about adding options to a package in order to change default values of some functions on-the-fly

4 messages · Samuel, Alexandre Courtiol, David Hugh-Jones +1 more

#
Hi,

I would like to change the default value of some arguments of some 
functions in a package of mine. I don't want to change explicitly the 
calls in the many scripts that have been written. For example, I would 
to change the delimiter in all write.mytable() without changing any 
calls it and without changing the default value currently assigned to 
that delimiter in the declaration of this function. What I would like is 
to add a command at the beginning of the scripts that says "since now, 
the delimiter is ;".

I am thinking about setting global options that those functions will 
read and use to overwrite the default values. Of course the 
write.mytable function has to be modify to exploit these settings, but 
none of the scripts. I think about something similar to the par() and 
options() functions.

I noticed the settings package, but I would like to know if there is a 
simple and recommended solution without adding any dependency.

Hope my question is clear.

Thanks for any advice,
Samuel
#
Dear Samuel,

Many may object (for good reasons) that adding options would clash with
functional paradigm, but it is possible.
I don't know about the least worse practice but as far as I would do it,
you could:

1. directly write and then read elements in the (hidden) list .Options that
is present in the global environment:
options(test = "blabla") ## add element in .Options
.Options$test ## read element (for use in your own function)

2. you could also create your own alternative to options(), as ex:
https://github.com/courtiol/IsoriX/blob/master/IsoriX/R/options.R
Option 2 has the benefits of not risking conflict with existing names in
.Options and it would thus also not mess up the way other packages may work.
Option 1 uses what is already in place.

I hope this help,
Best

Alex
On Thu, 6 Sep 2018 at 17:18, Samuel <samuel.granjeaud at inserm.fr> wrote:

            

  
    
#
Hi all,

A simple solution - if indeed you want to go down this route - is to use
options() and getOption(), ensuring all options are namespaced, e.g. by
prefixing them with the package name.

David

On Thu, 6 Sep 2018 at 17:15, Alexandre Courtiol <
alexandre.courtiol at gmail.com> wrote:

            

  
  
#
On 09/07/2018 04:15 AM, Alexandre Courtiol wrote:
<SNIP>
<SNIP>

Point of order Mr. Chairman (and I'm pretty sure it doesn't matter a 
damn) but (on my system at least) .Options is created in the *base* 
package, not in the global environment.

E.g.:

# Freshly started R session.
 > find(".Options")
[1] "package:base"
 > options(mung="gorp")
 > find(".Options")
[1] "package:base"
 > options("mung")
$mung
[1] "gorp"
 > .Options$mung
[1] "gorp"

cheers,

Rolf Turner