Skip to contents

This function creates a cross product of multiple lists, similar to purrr::cross() but with a more convenient interface that doesn't require wrapping arguments in an extra list(). It's useful for generating combinations of parameters for analysis.

Usage

expand_list(...)

Arguments

...

Named arguments, each containing a vector or list of values to combine

Value

A list of lists, where each inner list contains one combination of values from the input arguments

Examples

# Create combinations of parameters
combinations <- plnr::expand_list(
  a = 1:2,
  b = c("a", "b")
)

# View the combinations
str(combinations)
#> List of 4
#>  $ :List of 2
#>   ..$ a: int 1
#>   ..$ b: chr "a"
#>  $ :List of 2
#>   ..$ a: int 2
#>   ..$ b: chr "a"
#>  $ :List of 2
#>   ..$ a: int 1
#>   ..$ b: chr "b"
#>  $ :List of 2
#>   ..$ a: int 2
#>   ..$ b: chr "b"

# Compare with purrr::cross
purrr::cross(list(
  a = 1:2,
  b = c("a", "b")
))
#> Warning: `cross()` was deprecated in purrr 1.0.0.
#>  Please use `tidyr::expand_grid()` instead.
#>  See <https://github.com/tidyverse/purrr/issues/768>.
#> [[1]]
#> [[1]]$a
#> [1] 1
#> 
#> [[1]]$b
#> [1] "a"
#> 
#> 
#> [[2]]
#> [[2]]$a
#> [1] 2
#> 
#> [[2]]$b
#> [1] "a"
#> 
#> 
#> [[3]]
#> [[3]]$a
#> [1] 1
#> 
#> [[3]]$b
#> [1] "b"
#> 
#> 
#> [[4]]
#> [[4]]$a
#> [1] 2
#> 
#> [[4]]$b
#> [1] "b"
#> 
#>