Skip to contents

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

A bare (class-less) list is recursed into; NULL contributes no leaves; everything else (a classed object, an atomic, a function, ...) is a leaf.

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"
#>