Visits every node in a parsed R expression, calling visitor callbacks for calls, symbols, and literals. Findings from callbacks are accumulated and returned.
Arguments
- expr
A language object (from
parse_code()orbase::parse()).- visitor
A list with optional callback functions:
on_callfunction(expr, fn_name, depth)– called for function calls.fn_nameis extracted viacall_fn_name().on_symbolfunction(expr, name, depth)– called for symbols (names).on_literalfunction(expr, depth)– called for literal values (numeric, character, logical, NULL, etc.).
Each callback should return
NULLto continue without accumulating, or any other value to add it to the findings list.- depth
Integer. Current nesting depth (used internally for recursion). Defaults to 0.
Examples
# Collect all function call names from an expression
expr <- parse_code("mean(x) + sum(y)")[[1]]
visitor <- list(
on_call = function(expr, fn_name, depth) fn_name
)
walk_ast(expr, visitor)
#> [[1]]
#> [1] "+"
#>
#> [[2]]
#> [1] "mean"
#>
#> [[3]]
#> [1] "sum"
#>