replace_nodes: Replace Nodes in an RStatic Object

View source: R/find_nodes.R

replace_nodesR Documentation

Replace Nodes in an RStatic Object

Description

This function traverses the given RStatic object, calls the given replace function on each node, and replaces each node with the result of the call.

Usage

replace_nodes(node, replace, ..., in_place = FALSE)

Arguments

node

(ASTNode) The RStatic object to traverse.

replace

(function) The function to call on each node.

...

Additional arguments to the replace function.

in_place

(logical) Copy node before replacement?

Details

Replacement happens from the bottom up, so node is replaced last.

See Also

find_nodes()

Examples

rename_symbols = function(node, name, newname)
{ # Rename symbols to something else.
  if (is(node, "Symbol") && node$value == name)
    node$value = newname

  node
}

ast = quote_ast(ans <- sum(x, y / x, z))

replace_nodes(ast, rename_symbols, "x", "newx")

nick-ulle/rstatic documentation built on May 14, 2024, 8:10 p.m.