mkGlobalsLocal | R Documentation |
This function analyzes a collection of functions and identifies the global variables each
uses and then adds parameters for each of these parameters to allow the caller
to override the global variable. The default value for each additional parameter we add is the original
global variable so that each function behaves the same, by default, but is no longer
constrained to use only those global variables.
For example, consider a function that uses a global variable a
function(x) x + a
. This is modified to be
function(x, .a = a) x + .a
.
This allows a caller to use f(10)
and use the global value of
a
or f(10, 4)
to override a
.
mkGlobalsLocal(..., .funs = list(...), .addDefaults = rep(TRUE, length(.funs)))
... |
zero or more function objects to be analyzed and modified |
.funs |
an alternative way to specify the collection of function objects |
.addDefaults |
a logical vector/value controlling whether the default value for the added parameters is the original corresponding global variable |
A list of updated functions.
Duncan Temple Lang (?)
getGlobals
f = function(x) x + a
f2 = mkGlobalsLocal(f)
formals(f)
formals(f2)
f
f2
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.