modify_tree | R Documentation |
modify_tree()
allows you to recursively modify a list, supplying functions
that either modify each leaf or each node (or both).
modify_tree(
x,
...,
leaf = identity,
is_node = NULL,
pre = identity,
post = identity
)
x |
A list. |
... |
Reserved for future use. Must be empty |
leaf |
A function applied to each leaf. |
is_node |
A predicate function that determines whether an element is
a node (by returning |
pre, post |
Functions applied to each node. |
Other modify variants:
map_depth()
,
modify()
x <- list(list(a = 2:1, c = list(b1 = 2), b = list(c2 = 3, c1 = 4)))
x |> str()
# Transform each leaf
x |> modify_tree(leaf = \(x) x + 100) |> str()
# Recursively sort the nodes
sort_named <- function(x) {
nms <- names(x)
if (!is.null(nms)) {
x[order(nms)]
} else {
x
}
}
x |> modify_tree(post = sort_named) |> str()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.