getGlobals | R Documentation |
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!)
getGlobals(f, expressionsFor = character(), .ignoreDefaultArgs = FALSE,
skip = c(".R", ".typeInfo", ".signature", ".pragma", ".Internal", ".Primitive"),
.debug = TRUE, .assert = TRUE, localVars = character(),
mergeSubFunGlobals = TRUE, old = TRUE,
indirectCallFunctions = names(getIndirectCallFunList()),
handleTextConnections = TRUE, availableVars = character())
getScriptGlobals(file, ...)
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. |
.debug |
whether ignore calls to |
.assert |
whether ignore calls to |
mergeSubFunGlobals |
a logical scalar value that
controls whether we merge the global functions used in any function
defined within this functions |
old |
a logical value controlling whether we use the old mechanism or the new mechanism. Will be removed in the future when we decide on correct approach. |
indirectCallFunctions |
a character vector specifying the names of
functions which treat one of the arguments as a function, e.g.,
|
handleTextConnections |
a logical value that controls whether we treat
calls to |
availableVars |
a character vector of variable names that the are to be considered available as variables the code can reference and not to be identified as non-local/global. |
file |
the full path to the script file or the parsed code from the script |
... |
additional arguments passed to |
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
|
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 |
skippedExpressions |
a list containing all of the call
expressions that were skipped based on the call contents of |
Duncan Temple Lang
findGlobals
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.