getGlobals: Compute information about uses of globals, etc. for a...

Description Usage Arguments Value Author(s) See Also Examples

Description

This is an alternative version of findGlobals in the codetools package for finding the names of global variables and functions used in the code of an R function. This version uses more context, has additional features we need, and also computes additional information that we use in compilation of R code in the RLLVMCompile package and which also may be useful for other purposes.

I also find this function easier to understand and adapt as it is more direct. (However, I wrote it so of course I understand it!)

Usage

1
2
3
4
getGlobals(f, expressionsFor = character(), .ignoreDefaultArgs = FALSE,
           skip = c(".R", ".typeInfo", ".signature", ".pragma"),
           .debug = TRUE, .assert = TRUE, localVars = character(),
           mergeSubFunGlobals = TRUE)

Arguments

f

the function to be analyzed

expressionsFor

names of R functions we are interested in so that we collect all calls to those functions in the body of this function

.ignoreDefaultArgs

a logical value which controls whether we ignore or process the code in the default values of the parameters/formal arguments of the function.

localVars

this is not intended to be provided by the caller. a character vector

skip

a character vector that allows us to ignore calls to particular functions, e.g. .R or .debug. These are typically functions that will be handled in a special way by, e.g., a compiler.

.debug

whether ignore calls to .debug or another debugging function. This can be a logical value to enable/disable skipping .debug calls, or it can be the name of another function, e.g. "myDebug" which we will add to the skip vector.

.assert

whether ignore calls to .assert or another assertion function. This can be a logical value to enable/disable skipping .assert calls, or it can be the name of another function, e.g. "assert_that" which we will add to the skip vector.

mergeSubFunGlobals

a logical scalar value that controls whether we merge the global functions used in any function defined within this functions f into the answer or keep them separate.

Value

A list of class GlobalUses with numerous elements:

localVariables

the names of all the variables that are assigned in the body of the function.

variables

the names of non-local/global variables referenced in this code.

functions

the names of non-local/global functions referenced in this code.

variablesByFun

a list with an element for each globally referenced function. Each element is a frequency table of the symbols (i.e., variables/functions) that are in the calls for this function. In other words, these are the symbols used in the calls to the external function.

expressions

a list with an element for each function named in expressionsFor that was actually invoked in the body of the function. Each such element is a collection of the expressions which called that function in expressionsFor.

subFunctions

a list with an element for each of the nested functions defined within the body of this code. Each element is the result from calling getGlobals on that function definition.

skippedExpressions

a list containing all of the call expressions that were skipped based on the call contents of skip.

Author(s)

Duncan Temple Lang

See Also

findGlobals

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
g =
function(a = x, b = y)
{
   x = 1
   y = 2
   a + b * (x + y)
}

getGlobals(g)$variables

tmp = substituteDefaultValues(g)
getGlobals(tmp, .ignoreDefaultArgs = TRUE)$variables


#############

account =
function(balance = 0L)
{
    deposit = function(amt)
                  balance <<- balance + amt
    withdrawl = function(amt)
                    balance <<- balance - amt

    list(deposit = deposit, withdraw = withdrawl,  balance = function() balance)
}
g

duncantl/CodeAnalysis documentation built on Jan. 20, 2020, 5:17 a.m.