Skip to contents

Recursively dis-embed objects in lists of lists to a single, linear list of list objects.

Usage

list_iron(
  ...,
  name_spec = "{outer}|{inner}",
  name_repair = c("minimal", "unique", "check_unique", "universal", "unique_quiet",
    "universal_quiet"),
  .f = identity
)

Arguments

...

objects or list of objects.

name_spec

If both inner and outer names are present, control how they are combined. Should be a glue specification that uses variables inner and outer.

name_repair

One of "minimal", "unique", "universal", or "check_unique". See vctrs::vec_as_names() for the meaning of these options.

.f

function to apply to every object in the list; default function is identity.

Value

A flat named list of objects

Details

  • If object is not in a list, list_iron places the object in a list.

  • Removes all empty embedded lists.

  • Removes empty comma-separated arguments.

Because spreadsheets tabs have no hierarchical structure, any list of data objects to prepare for presentation within a workbook must be flattened to sheets. This function is a precursor for printing listed data objects with xlr.

list_iron forces all objects in an embedded object list to become a single-file list, and optionally conform to a function passed by the user, e.g. entibble(). list_iron acts similar to purrr::list_flatten(), but collapses all embedded lists rather than a single layer. Also concatenates embedded-list names via parameter name_spec.

Examples

head(iris) |> list_iron()
#> $`head(iris)`
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#> 
head(iris) |> list() |> list() |> list() |> list_iron()
#> $`list(list(list(head(iris))))`
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#> 
list_iron(list(list(iris)), mtcars, .f = ~tail(., 2))
#> $`list(list(iris))`
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#> 149          6.2         3.4          5.4         2.3 virginica
#> 150          5.9         3.0          5.1         1.8 virginica
#> 
#> $mtcars
#>                mpg cyl disp  hp drat   wt qsec vs am gear carb
#> Maserati Bora 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
#> Volvo 142E    21.4   4  121 109 4.11 2.78 18.6  1  1    4    2
#> 

# Compare list structure to purrr::list_flatten():
a_list <- list(list(1, list(), 2, list(3)))
a_list |> str()
#> List of 1
#>  $ :List of 4
#>   ..$ : num 1
#>   ..$ : list()
#>   ..$ : num 2
#>   ..$ :List of 1
#>   .. ..$ : num 3
a_list |> purrr::list_flatten() |> str()
#> List of 4
#>  $ : num 1
#>  $ : list()
#>  $ : num 2
#>  $ :List of 1
#>   ..$ : num 3
a_list |> list_iron() |> str()
#> List of 3
#>  $ a_list|1: num 1
#>  $ a_list|2: num 2
#>  $ a_list|3: num 3

# Naming examples:
messy_list <- list(list(list(1:5), a = list(5:1, 'green', list('blue')), letters))
messy_list |> str()
#> List of 1
#>  $ :List of 3
#>   ..$  :List of 1
#>   .. ..$ : int [1:5] 1 2 3 4 5
#>   ..$ a:List of 3
#>   .. ..$ : int [1:5] 5 4 3 2 1
#>   .. ..$ : chr "green"
#>   .. ..$ :List of 1
#>   .. .. ..$ : chr "blue"
#>   ..$  : chr [1:26] "a" "b" "c" "d" ...
messy_list |> list_iron() |> str()
#> List of 5
#>  $ messy_list|   : int [1:5] 1 2 3 4 5
#>  $ messy_list|a|1: int [1:5] 5 4 3 2 1
#>  $ messy_list|a|2: chr "green"
#>  $ messy_list|a|3: chr "blue"
#>  $ messy_list|   : chr [1:26] "a" "b" "c" "d" ...
messy_list |> list_iron(name_repair = 'unique') |> names()
#> [1] "messy_list|...1" "messy_list|a|1"  "messy_list|a|2"  "messy_list|a|3" 
#> [5] "messy_list|...5"
messy_list |> list_iron(name_spec = '', name_repair = 'unique') |> names()
#> [1] "...1" "...2" "...3" "...4" "...5"
messy_list |> list_iron(name_spec = '{outer}', name_repair = 'unique') |> names()
#> [1] "messy_list...1" "messy_list...2" "messy_list...3" "messy_list...4"
#> [5] "messy_list...5"