Reconstructs a nested structure from a flat list by using a tree previously
created with build_tree(). Each LeafNode in the tree selects the
corresponding element from x by index, and ListNodes restore the
original nesting and names.
Arguments
- node
(
Node)
Tree describing the target structure, as returned bybuild_tree().- x
(list)
Flat list of leaf values, typically produced byflatten().
Examples
x <- list(a = 1, b = list(c = 2, d = 3))
tree <- build_tree(x)
flat <- flatten(x)
unflatten(tree, flat)
#> $a
#> [1] 1
#>
#> $b
#> $b$c
#> [1] 2
#>
#> $b$d
#> [1] 3
#>
#>
unflatten(tree, list(10, 20, 30))
#> $a
#> [1] 10
#>
#> $b
#> $b$c
#> [1] 20
#>
#> $b$d
#> [1] 30
#>
#>