Skip to contents

Recursively flattens a nested list into a single flat list containing only the leaf values, preserving left-to-right order.

Currently only lists are flattened and all other objects are treated as leaves.

Use build_tree() to capture the nesting structure so it can be restored with unflatten().

Usage

flatten(x)

Arguments

x

(any)
Object to flatten.

Value

list() containing the flattened values.

Examples

x <- list(a = 1, b = list(c = 2, d = 3))
flatten(x)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 

flatten(list(1:3, "hello"))
#> [[1]]
#> [1] 1 2 3
#> 
#> [[2]]
#> [1] "hello"
#>