substituteDefaultValues: Insert code for default values into the body of the code

substituteDefaultValuesR Documentation

Insert code for default values into the body of the code

Description

This function creates a modified version of the specified function by adding code to compute the default value of each parameter just before it is first used. This facilitates compiling code and code analysis to determine global variables correctly. It honors lazy evaluation by not evaluating the code until the variable is referenced.

Usage

substituteDefaultValues(f, removeDefaults = TRUE,
                        sc = new("Script", as.list(body(f))[-1]), info = getInputs(sc))

Arguments

f

the function to process

removeDefaults

a logical value that controls whether we remove the original default value expressions in the formals of the returned function.

sc

a Script object created via the CodeDepends package. The caller does not need to specify this, but can if they have computed the Script object already for the code in the body of the function.

info

like sc, this is input computed via the CodeDepends package from the Script object and which contains the input/output information for each expression in the body of the code.

Value

A function. This is the modified version of the original function.

Note

It is possible that the semantics of the new function may differ from the original version in the case that the order of evaluation of the arguments may be different. This might occur in the case that two arguments are missing and we evaluate their default values in a specific order that is different from how they are evaluated in the actual call to another function. For example, in foo(x, y), with x and y missing in the calling function, we would first evaluate x and then y via their default via expressions. However, if foo(x, y) actually uses y first, then our order would be incorrect.

Author(s)

Duncan Temple Lang

See Also

getGlobals

Examples

g =
function(a = x, b = y)
{
   x = 1
   y = 2
   a + b * (x + y)
}

substituteDefaultValues(g)

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