replace_nodes: Replace Nodes in an RStatic Object

Description Usage Arguments Details See Also Examples

View source: R/find_nodes.R

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

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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/ast documentation built on Oct. 18, 2019, 4:37 a.m.