pmap()

  • you can pass a named list or dataframe as arguments to a function

  • for example runif() has the parameters n, min and max

params <- tibble::tribble(
  ~ n, ~ min, ~ max,
   1L,     1,    10,
   2L,    10,   100,
   3L,   100,  1000
)

pmap(params, runif)
#> [[1]]
#> [1] 8.061177
#> 
#> [[2]]
#> [1] 10.84869 80.11593
#> 
#> [[3]]
#> [1] 756.4516 667.1187 532.8197
  • could also be
list(
  n = 1:3, 
  min = 10 ^ (0:2), 
  max = 10 ^ (1:3)
) |> 
pmap(runif)
#> [[1]]
#> [1] 2.409732
#> 
#> [[2]]
#> [1] 10.73940 50.72126
#> 
#> [[3]]
#> [1] 543.0640 450.6284 518.1993
  • I like to use expand_grid() when I want all possible parameter combinations.
expand_grid(n = 1:3,
            min = 10 ^ (0:1),
            max = 10 ^ (1:2))
#> # A tibble: 12 × 3
#>        n   min   max
#>    <int> <dbl> <dbl>
#>  1     1     1    10
#>  2     1     1   100
#>  3     1    10    10
#>  4     1    10   100
#>  5     2     1    10
#>  6     2     1   100
#>  7     2    10    10
#>  8     2    10   100
#>  9     3     1    10
#> 10     3     1   100
#> 11     3    10    10
#> 12     3    10   100

expand_grid(n = 1:3,
            min = 10 ^ (0:1),
            max = 10 ^ (1:2)) |> 
pmap(runif)
#> [[1]]
#> [1] 7.419511
#> 
#> [[2]]
#> [1] 6.474891
#> 
#> [[3]]
#> [1] 10
#> 
#> [[4]]
#> [1] 41.93048
#> 
#> [[5]]
#> [1] 8.22531 8.52138
#> 
#> [[6]]
#> [1] 24.53719 36.04462
#> 
#> [[7]]
#> [1] 10 10
#> 
#> [[8]]
#> [1] 87.11969 86.83870
#> 
#> [[9]]
#> [1] 3.663059 2.323435 7.335929
#> 
#> [[10]]
#> [1] 11.27686  4.33905 99.94105
#> 
#> [[11]]
#> [1] 10 10 10
#> 
#> [[12]]
#> [1] 13.13873 40.45522 92.35574