node-traversal | R Documentation |
This is a collection of functions used to navigate the tree. Some of
them have a variant that applies on a single node (e.g. node_next()
) and
one that applies on a list of nodes (e.g. node_next_all()
):
node_prev()
, node_prev_all()
, node_next()
, and node_next_all()
get the previous and next node(s) that are at the same depth as the current
node;
node_parent()
, node_ancestors()
, node_child()
and node_children()
get the node(s) that are above or below the current node in terms of depth.
All nodes except the root node have at least one node (the root).
node_parent(x)
node_child(x, nth)
node_ancestors(x)
node_children(x)
node_next(x)
node_next_all(x)
node_prev(x)
node_prev_all(x)
x |
A node, either from |
nth |
Integer. The child node to find. This is 0-indexed, so setting
|
A node
### get the previous/next node ---------------------------
src <- "
print('hi there')
a <- 1
fn <- function(x) {
x + 1
}
"
root <- src |>
tree_new() |>
tree_root()
root |>
node_find(ast_rule(pattern = "a <- $A")) |>
node_prev() |>
node_text()
root |>
node_find(ast_rule(pattern = "a <- $A")) |>
node_next() |>
node_text()
# there are nodes inside the function, but there are no more nodes on the
# same level as "fn"
root |>
node_find(ast_rule(pattern = "a <- $A")) |>
node_next_all() |>
node_text_all()
### get the parent/child node ---------------------------
src <- "
print('hi there')
a <- 1
fn <- function(x) {
x + 1
}
"
root <- src |>
tree_new() |>
tree_root()
root |>
node_find(ast_rule(pattern = "$VAR + 1")) |>
node_parent() |>
node_text()
root |>
node_find(ast_rule(pattern = "$VAR + 1")) |>
node_ancestors() |>
node_text_all()
root |>
node_find(ast_rule(pattern = "$VAR + 1")) |>
node_child(0) |>
node_text()
root |>
node_find(ast_rule(pattern = "$VAR + 1")) |>
node_children() |>
node_text_all()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.