getAllCalls: Get all function calls in R code

View source: R/freeVariables.R

getAllCallsR Documentation

Get all function calls in R code

Description

This statically analyzes the R code and finds all calls both at the top-level in a script and in functions, in a directory of R files, etc.

One situation in which this can be useful is to identify which packages need to be loaded/imported for a collection of files, e.g., when creating a package from existing files, or knowing which calls to link[base]{library} are needed.

getAllSymbols gets all the symbols in an R language object.

Usage

getAllCalls(x, ...)
getAllSymbols(expr, unique = TRUE, predicate = is.symbol)

Arguments

x

a character vector of file or directory names or the parsed code

...

arguments for methods

expr

the R code/language object

unique

a scalar logical value controlling whether all occurrences of symbols are returned (FALSE) or just the set of unique symbols that occurred in the code.

predicate

the function that determines whether the language object is a symbol and one we want to capture. This can be more specific such as function(x) is.symbol(x) && as.character(x) %in% c("foo", "bar")

Value

A list of rstatic Call objects.

Author(s)

Duncan Temple Lang, Nick Ulle

References

rstatic package https://github.com/nick-ulle/rstatic.git

See Also

findCallsTo

findLiterals

Examples

## Not run: 
  # calls by file in the directory R/
  f = getAllCalls("R")
  # flatten to a list of all calls
  k = unlist(f, recursive = FALSE)
  # What is the class
  table(sapply(k, function(x) class(x$fn)[1]))
  funs = sapply(k[sapply(k, function(x) is(x$fn, "Symbol"))], function(x) x$fn$value)
  table(funs[ !(funs 

## End(Not run)

duncantl/CodeAnalysis documentation built on March 1, 2025, 9:54 p.m.