Skip to contents

Use in place of list to produce a list with auto-assigned names and to avoid adding list structure.

Usage

enlist(..., .label = NULL)

Arguments

...

data objects; if unnamed, enlist() forces names by input expression

.label

a function or character vector to rename list elements, e.g. .label = ~substr(.,1,5). .label is passed to nm in 'rlang::set_names()'

Value

a named list

Details

  • Creates a list where each input list element is force-named by the assigned name or input expression, .e.g enlist(mtcars).

  • If no list exists, enlist places its arguments in a list, e.g. enlist(c('hi', 'hello')).

  • If passed a bare list, enlist does not add an additional list layer, e.g. enlist(list()). That is, enlist() doesn't stack lists for structure's sake alone, e.g. enlist(enlist(letters)). In this dis-embedding case, a user provided name passed with =, like enlist(some_name = list('hi')), may be discarded if there is no place for it after dis-embedding, e.g. enlist(some_name = list('hi', 'bye')).

  • Handles dots, splicing, and injection, e.g. enlist(!!!letters).

  • Ignores input argument separator commas, e.g. enlist(,,'hi',,,'world').

  • Naming the output can be performed by function through the parameter .label, e.g. enlist('me', .label = ~paste0('name_',.))

Examples

# Examples compare `enlist()` to `list()`:

# Auto-naming by input expression:
enlist(head(iris), tail(mtcars))
#> $`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
#> 
#> $`tail(mtcars)`
#>                 mpg cyl  disp  hp drat    wt qsec vs am gear carb
#> Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
#> Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
#> Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
#> Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
#> Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2
#> 
list(head(iris), tail(mtcars))
#> [[1]]
#>   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
#> 
#> [[2]]
#>                 mpg cyl  disp  hp drat    wt qsec vs am gear carb
#> Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
#> Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
#> Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
#> Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
#> Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
#> Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2
#> 

# Naming a list element works similar to `list()`:
enlist(some_name = letters)
#> $some_name
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
list(some_name = letters)
#> $some_name
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 

# Naming by passing a function to `.label`:
enlist(letters, .label =  'some_name')
#> $some_name
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
enlist(letters, .label =  ~paste0(.,'_1'))
#> $letters_1
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
enlist('black','white','cyan', .label =  'color_grp_1')
#> $color_grp_1
#> [1] "black"
#> 
#> $color_grp_1
#> [1] "white"
#> 
#> $color_grp_1
#> [1] "cyan"
#> 
enlist(letters, .label =  ~'')  # removes names
#> [[1]]
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 


# List embedding:
enlist(enlist(enlist(letters))) # 1 list deep
#> $letters
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
list(list(list(letters))) # 3 lists deep
#> [[1]]
#> [[1]][[1]]
#> [[1]][[1]][[1]]
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
#> 
#> 

list(letters, b = enlist(a = letters, 'blue')) |> enlist()
#> [[1]]
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
#> $b
#> $b$a
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
#> 
#> $b$blue
#> [1] "blue"
#> 
#> 

# Handles non-standard evaluation:
candy <- list('lollipops','gum')
enlist(candy, !!!candy)
#> $candy
#> $candy[[1]]
#> [1] "lollipops"
#> 
#> $candy[[2]]
#> [1] "gum"
#> 
#> 
#> $lollipops
#> [1] "lollipops"
#> 
#> $gum
#> [1] "gum"
#>