findCallsToFunctions | R Documentation |
These analyze R code and find all calls to functions in the given set of interest and either returns these or the specified argument(s), either by position or name.
For findCallsToFunctions
, if argIndices has length zero, the calls are returned.
Otherwise, the corresponding parameters from each call are returned.
findCallsTo
uses the regular R AST, not rstatic
's
and returns the R language objects for the call.
numCalls
is a simple derivative of findCallsTo
and computes the number of calls in a function. This gives
a sense of the complexity of the code.
findCallsTo(code, funNames = character(),
indirectCallFuns = getIndirectCallFunList(),
walker = if(!missing(rx))
mkCallWalkerPred(findCallsRXPred(rx), skipIfFalse = FALSE)
else
mkCallWalker(funNames, indirect = indirectCallFuns, skipIfFalse = skipIfFalse),
parse = any(!sapply(funNames, is.name)), skipIfFalse = TRUE,
rx = character())
findCallsToFunctions(allCalls, funNames, argIndices = integer(), definitions = NULL)
isCallTo(code, funName, indirect = getIndirectCallFunList(), isLHS = NA)
mkCallWalkerPred(pred, ..., skipIfFalse = TRUE)
numCalls(code)
getIndirectCallFunList(..., .els = list(...))
code |
the R code to analyze, or path to an R source file or directory containing R code |
funNames , funName |
the names of functions or variables of interest. This can be a character vector of names or of R calls/expressions, or a list of previously parsed call language objects. |
rx |
a regular expression used to identify functions of interest, rather than actual names |
indirect , indirectCallFuns |
a named character vector or a logical value.
If |
walker |
the |
parse |
whether to parse the |
skipIfFalse |
a logical value. If |
allCalls |
a character vector of file or directory names or parsed R code or list of calls |
argIndices |
an integer or character vector identifying the parameters of interest in each of the calls |
definitions |
a list of functions that provides the definitions
for functions that are not available on the search path. These are
used in |
pred |
a function that takes two arguments - a call and a logical value indicating if the function being called is a symbol/name or itself a call - and returns a logical scalar value indicating whether to collect and return the specific call. The |
... |
additional arguments passed to the |
.els |
a named-list identifying functions that have a parameter/formal argument
that is expected to be a function that it will call, e.g., |
isLHS |
a logical value. Intended to indicate whether the call |
\
findCallsTo
returns a list of the call language objects.
mkCallWalkerPred
returns a list with 3 elements which are functions:
leaf |
walks the different elements of the AST |
call |
processes a call language object and determines whether to add it to the results |
handler |
returns NULL |
isCallTo
returns a scalar logical value - TRUE
or FALSE
.
If argIndices
has length zero, a list of all the matching
Call
objects.
Otherwise, a list of all the parameters in each matching call
identified by argIndices
either by position or name.
getIndirectCallFunList
returns a named character vector
with the values being the parameter names and the names being
the function with the parameter.
Duncan Temple Lang
egFun = findCallsTo
findCallsTo(egFun, c("lapply", "walkCode"))
findCallsTo(getNamespace("parallel"), "eval")
isLApplyWalkCodeCall = function(x, isName, ...) isSymbol(x[[1]], "lapply") && isSymbol(x[[3]], "walkCode")
findCallsTo(egFun, walker = mkCallWalkerPred(isLApplyWalkCodeCall))
isLApplyWalkCodeCall = function(x, isName, ...) isCallTo(x, "lapply") && isSymbol(x[[3]], "walkCode")
findCallsTo(egFun, walker = mkCallWalkerPred(isLApplyWalkCodeCall))
f = system.file("sampleCode/source.R", package = "CodeAnalysis")
a = findCallsTo(f, "source")
b = findCallsTo(f, "source", skipIfFalse = FALSE)
setdiff(b, a)
# Regular expression
z = findCallsTo(findCallsTo, rx = "is\\.")
z = findCallsTo(findCallsTo, rx = "file.info")
z = findCallsTo(findCallsTo, rx = "apply")
# Compare matching any function name and no function names.
z = findCallsTo(findCallsTo, rx = ".*")
z2 = findCallsTo(findCallsTo)
setdiff(z2, z)
# The regular expression does not include the walker$ans() call
# since the findCallsTo() with no function names includes all calls.
## Not run:
# Project from git@github.com:mespe/Variety_trial_analysis.git
ff = list.files("~/Book/ExploreCode/Variety_trial_analysis/code", pattern = "\.R$", full = TRUE)
e = unlist(lapply(ff, parse), recursive = FALSE)
k = findCallsTo(e)
z = sapply(k, function(x) deparse(x[[1]]))
sort(table(z), decreasing = TRUE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.