mkGlobalsLocal: Modify a function to make the global variables additional...

View source: R/passGlobals.R

mkGlobalsLocalR Documentation

Modify a function to make the global variables additional parameters

Description

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.

Usage

mkGlobalsLocal(..., .funs = list(...), .addDefaults = rep(TRUE, length(.funs)))

Arguments

...

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

Value

A list of updated functions.

Author(s)

Duncan Temple Lang (?)

See Also

getGlobals

Examples

f = function(x) x + a
f2 = mkGlobalsLocal(f)
formals(f)
formals(f2)
f
f2

duncantl/CodeAnalysis documentation built on Feb. 21, 2024, 10:49 p.m.