Build the call tree of every function defined in a directory of R files
call_tree_dir.Rdcall_tree_dir() parses every .R file in path without evaluating any
of it, extracts each top-level function definition
(name <- function(...) ...) and the methods of each R6 class
(Name <- R6Class(...)), and builds the call graph among those
locally-defined functions. The result is the tree of calls reachable
from root, in the same shape call_stack() returns, so it prints the
same way.
Unlike call_stack(), which resolves an already-loaded function through
R's own namespace lookup, call_tree_dir() never loads or runs the
scanned files. That makes it usable on a package's R/ directory, or a
Shiny app's source, without installing or sourcing it first.
Usage
call_tree_dir(
path = "R",
root,
modules_only = FALSE,
exclude = NULL,
max_depth = Inf,
keep = NULL,
prune = NULL,
include_ns = NULL,
include_defaults = FALSE
)Arguments
- path
Character scalar; directory containing
.Rfiles. Default"R".- root
Character scalar; name of the function to root the tree at, or an R6 method written as
"Class$method"("Class$new"forinitialize). Must be one of the functions found inpath; otherwisecall_tree_dir()errors rather than returning an empty tree.- modules_only
Logical. When
TRUE, prune the tree toroot, the functionsrootcalls directly, and functions that callshiny::NS()orshiny::moduleServer()(Shiny modules); everything else is spliced out, promoting its children so modules reached through a non-module wrapper function stay visible. DefaultFALSE, which keeps every locally-defined function reachable fromroot.- exclude
Character vector of function names to drop from the tree, e.g. a logging helper such as
"logr_msg"that appears under every node. A dropped function's children are spliced up into its place, as withmodules_only. The root is never dropped. DefaultNULL, which drops nothing.- max_depth
Number of levels to show below
root. DefaultInf, which shows the whole tree;1shows only the functionsrootcalls. Counted aftermodules_only,exclude,prune, andkeepare applied.- keep
Character vector of regular expressions, e.g.
"^mod_counts_". When given, the tree is trimmed to nodes whose name matches one of them (with their full subtrees) plus the nodes on the path fromrootdown to each match. Use"^name$"for an exact name. DefaultNULL, which keeps everything.- prune
Character vector of function names to drop together with everything below them. Unlike
exclude, children are not spliced up. DefaultNULL, which drops nothing.- include_ns
Character vector of package names, e.g.
c("shiny", "bslib"). Calls to functions exported by these packages (bare, aspkg::fun(), orpkg:::fun()) are shown as nodes, nested by how the calls are written:shinyApp(ui = app_ui, server = app_server)putsapp_uiandapp_serverundershinyApp. External functions are not followed into their own source, and syntax such as braces,<-, orifis never shown (so"base"is usable). The packages must be installed. DefaultNULL, which shows only locally-defined functions.- include_defaults
Logical. When
TRUE, calls in default argument values (function(dir = util_dir())) are included alongside calls in the body. DefaultFALSE.
Value
An object of class call_stack (see call_stack()).
Details
When modules_only = TRUE, the functions root calls directly (such as
app_ui() and app_server()) are always kept, so UI and server modules
stay grouped under them.
R6 methods are nodes named Class$method, with initialize shown as
Class$new. Class$new() calls that node. self$m(), private$m(),
and super$m() resolve within the class, following inherit. A
variable assigned Class$new(...) inside a function (x <- Class$new()) resolves x$m() to Class$m. With modules_only, a
class with any method that calls NS() or moduleServer() is a
module, and its $new and public methods are kept. The name after $
is never read as a call to a function of the same name.
A function defined more than once in path raises a warning naming the
files; the definition parsed last (files in alphabetical order) is used.
See also
call_tree_app() for the equivalent on a Shiny app object.
Examples
if (FALSE) { # \dontrun{
call_tree_dir("R", root = "launch_app")
call_tree_dir("R", root = "launch_app", modules_only = TRUE)
call_tree_dir("R", root = "launch_app", exclude = "logr_msg")
call_tree_dir("R", root = "nav_ui", keep = "^mod_counts_")
call_tree_dir("R", root = "launch_app", include_ns = "shiny", max_depth = 2)
} # }