Skip to contents

call_stack() takes the name of a function, optionally namespaced (e.g. "dplyr::select"), and statically parses its body. It then recursively resolves every function it calls, building a tree of those calls, the calls those functions make, and so on.

Resolution is static (read from the function's source code), not a runtime trace: the function is never called. UseMethod() dispatch is resolved by looking up the generic's registered S3 methods and following each one. Other dynamic calls (NextMethod(), S4 and R6 dispatch, and functions passed as data to do.call(), match.fun(), or Recall()) show as leaves rather than being followed further.

Usage

call_stack(fn, max_depth = Inf, exclude = NULL)

Arguments

fn

Character string naming a function, e.g. "dplyr::select", "pkg:::internal_fun", or "mean".

max_depth

Maximum recursion depth. Default Inf, which follows every call down to a primitive or a cycle already visited.

exclude

Character vector of function names to drop from the tree, e.g. a logging helper that appears under every node. A dropped function's children are spliced up into its place. The root is never dropped. Default NULL, which drops nothing.

Value

An object of class call_stack: a named list with name (the function name) and calls (a list of child nodes with the same shape).

See also

print.call_stack() for how the tree is drawn.

Examples

call_stack("mean.default", max_depth = 1)
#> █─mean.default
#> ├─.Internal
#> ├─anyNA
#> ├─c
#> ├─floor
#> ├─is.complex
#> ├─is.logical
#> ├─is.na
#> ├─is.numeric
#> ├─isTRUE
#> ├─length
#> ├─return
#> ├─sort.int
#> ├─stop
#> ├─unique
#> └─warning

# a generic: UseMethod() is replaced by the methods it dispatches to
call_stack("mean", max_depth = 1)
#> █─mean
#> ├─mean.Date
#> ├─mean.POSIXct
#> ├─mean.POSIXlt
#> ├─mean.default
#> ├─mean.difftime
#> ├─mean.quosure
#> └─mean.vctrs_vctr

# drop noise calls
call_stack("mean.default", max_depth = 1, exclude = "warning")
#> █─mean.default
#> ├─.Internal
#> ├─anyNA
#> ├─c
#> ├─floor
#> ├─is.complex
#> ├─is.logical
#> ├─is.na
#> ├─is.numeric
#> ├─isTRUE
#> ├─length
#> ├─return
#> ├─sort.int
#> ├─stop
#> └─unique