node-find | R Documentation |
Those functions find one or several nodes based on some rule:
node_find()
returns the first node that is found;
node_find_all()
returns a list of all nodes found.
Some arguments (such as kind
) require some knowledge of the tree-sitter
grammar of R. This grammar can be found here:
https://github.com/r-lib/tree-sitter-r/blob/main/src/grammar.json.
node_find(x, ..., files = NULL)
node_find_all(x, ..., files = NULL)
x |
A node, either from |
... |
Any number of rules created with |
files |
A vector of filenames containing rules. Those must be |
node_find()
returns a single SgNode
.
node_find_all()
returns a list of SgNode
s.
src <- "x <- rnorm(100, mean = 2)
any(duplicated(y))
plot(mtcars)
any(duplicated(x))"
root <- src |>
tree_new() |>
tree_root()
root |>
node_find(ast_rule(pattern = "any(duplicated($A))"))
root |>
node_find_all(ast_rule(pattern = "any(duplicated($A))"))
# using the 'kind' of the nodes to find elements
src <- "
a <- 1
while (TRUE) { print('a') }
"
root <- src |>
tree_new() |>
tree_root()
root |>
node_find(ast_rule(kind = "while_statement"))
# one can pass several rules at once
src <- "x <- rnorm(100, mean = 2)
any(duplicated(y))
plot(mtcars)
any(duplicated(x))
while (TRUE) { print('a') }"
root <- src |>
tree_new() |>
tree_root()
root |>
node_find(
ast_rule(pattern = "any(duplicated($A))"),
ast_rule(kind = "while_statement")
)
root |>
node_find_all(
ast_rule(pattern = "any(duplicated($A))"),
ast_rule(kind = "while_statement")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.