Captures the nesting structure of an object as a tree of Nodes. Each leaf
in the input becomes a LeafNode with an integer index corresponding to its
position in the flat list produced by flatten(). Lists become ListNodes
that record child nodes and names. The resulting tree can be passed to
unflatten() to reconstruct the original structure from a flat list.
Usage
build_tree(x, counter = NULL)
Arguments
- x
(any)
Object whose structure to capture. Lists are recursed into; everything else
is a leaf.
- counter
(NULL | environment)
Internal counter for assigning leaf indices. Mostly used internally and otherwise left as
NULL (default.)
Value
A Node (LeafNode for scalars, ListNode for lists).
Examples
x <- list(a = 1, b = list(c = 2, d = 3))
tree <- build_tree(x)
tree_size(tree)
#> [1] 3
flat <- flatten(x)
unflatten(tree, flat)
#> $a
#> [1] 1
#>
#> $b
#> $b$c
#> [1] 2
#>
#> $b$d
#> [1] 3
#>
#>