Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

walk_ast

Static code analysis for tarchetypes.


Description

Walk an abstract syntax tree and capture data.

Usage

walk_ast(expr, walk_call)

Arguments

expr

A language object or function to scan.

walk_call

A function to handle a specific kind of function call relevant to the code analysis at hand.

Details

For internal use only. Not a user-side function. Powers functionality like automatic detection of tar_load()/tar_read() dependencies in tar_render(). Packages codetools and CodeDepends have different (more sophisticated and elaborate) implementations of the concepts documented at https://adv-r.hadley.nz/expressions.html#ast-funs.

Value

A character vector of data found during static code analysis.

Examples

# How tar_render() really works:
expr <- quote({
  if (a > 1) {
    tar_load(target_name)
  }
  process_stuff(target_name)
})
walk_ast(expr, walk_call_knitr)
# Custom code analysis for developers of tarchetypes internals:
walk_custom <- function(expr, counter) {
  # New internals should use deparse_safe(backtick = FALSE).
  name <- deparse(expr[[1]])
  if (identical(name, "detect_this")) {
    counter_set_names(counter, as.character(expr[[2]]))
  }
}
expr <- quote({
  for (i in seq_len(10)) {
    for (j in seq_len(20)) {
      if (i > 1) {
        detect_this("prize")
      } else {
        ignore_this("penalty")
      }
    }
  }
})
walk_ast(expr, walk_custom)

tarchetypes

Archetypes for Targets

v0.2.0
MIT + file LICENSE
Authors
William Michael Landau [aut, cre] (<https://orcid.org/0000-0003-1878-3253>), Samantha Oliver [rev] (<https://orcid.org/0000-0001-5668-1165>), Tristan Mahr [rev] (<https://orcid.org/0000-0002-8890-5116>), Eli Lilly and Company [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.