This pair of functions make it easier to combine a function and list of parameters to get a result. invoke is a wrapper around do.call that makes it easy to use in a pipe. invoke_map makes it easier to call lists of functions with lists of parameters.

invoke(.f, .x = NULL, ..., .env = NULL)

invoke_map(.f, .x = list(NULL), ..., .env = NULL)

invoke_map_lgl(.f, .x = list(NULL), ..., .env = NULL)

invoke_map_int(.f, .x = list(NULL), ..., .env = NULL)

invoke_map_dbl(.f, .x = list(NULL), ..., .env = NULL)

invoke_map_chr(.f, .x = list(NULL), ..., .env = NULL)

invoke_map_dfr(.f, .x = list(NULL), ..., .env = NULL)

invoke_map_dfc(.f, .x = list(NULL), ..., .env = NULL)

Arguments

.f

For invoke, a function; for invoke_map a list of functions.

.x

For invoke, an argument-list; for invoke_map a list of argument-lists the same length as .f (or length 1). The default argument, list(NULL), will be recycled to the same length as .f, and will call each function with no arguments (apart from any suppled in ....

...

Additional arguments passed to each function.

.env

Environment in which do.call() should evaluate a constructed expression. This only matters if you pass as .f the name of a function rather than its value, or as .x symbols of objects rather than their values.

See also

Other map variants: imap, lmap, map2, map, modify

Examples

# Invoke a function with a list of arguments invoke(runif, list(n = 10))
#> [1] 0.38870131 0.97554784 0.28989230 0.67838043 0.73531960 0.19595673 #> [7] 0.98053967 0.74152153 0.05144628 0.53021246
# Invoke a function with named arguments invoke(runif, n = 10)
#> [1] 0.69582388 0.68855600 0.03123033 0.22556253 0.30083081 0.63646561 #> [7] 0.47902455 0.43217126 0.70643384 0.94857658
# Combine the two: invoke(paste, list("01a", "01b"), sep = "-")
#> [1] "01a-01b"
# That's more natural as part of a pipeline: list("01a", "01b") %>% invoke(paste, ., sep = "-")
#> [1] "01a-01b"
# Invoke a list of functions, each with different arguments invoke_map(list(runif, rnorm), list(list(n = 10), list(n = 5)))
#> [[1]] #> [1] 0.18033877 0.21689988 0.68016292 0.49884561 0.64167935 0.66028435 #> [7] 0.09602416 0.76560016 0.76967480 0.99071231 #> #> [[2]] #> [1] 1.88850493 -0.09744510 -0.93584735 -0.01595031 -0.82678895 #>
# Or with the same inputs: invoke_map(list(runif, rnorm), list(list(n = 5)))
#> [[1]] #> [1] 0.06521611 0.35420680 0.82519942 0.27381825 0.57004495 #> #> [[2]] #> [1] -0.4241750 -0.8723159 0.1066846 -0.5870140 -0.3278536 #>
invoke_map(list(runif, rnorm), n = 5)
#> [[1]] #> [1] 0.46598719 0.39003139 0.02006522 0.37697093 0.55991284 #> #> [[2]] #> [1] 1.06730788 0.07003485 -0.63912332 -0.04996490 -0.25148344 #>
# Or the same function with different inputs: invoke_map("runif", list(list(n = 5), list(n = 10)))
#> [[1]] #> [1] 0.67176682 0.05861411 0.99706914 0.14903547 0.51855664 #> #> [[2]] #> [1] 0.84612005 0.71826972 0.24131402 0.54704337 0.83480182 0.02795603 #> [7] 0.46938430 0.80568003 0.81405131 0.40391100 #>
# Or as a pipeline list(m1 = mean, m2 = median) %>% invoke_map(x = rcauchy(100))
#> $m1 #> [1] -17.1555 #> #> $m2 #> [1] -0.2478571 #>
list(m1 = mean, m2 = median) %>% invoke_map_dbl(x = rcauchy(100))
#> m1 m2 #> -1.0554082 -0.1491601
# Note that you can also match by position by explicitly omitting `.x`. # This can be useful when the argument names of the functions are not # identical list(m1 = mean, m2 = median) %>% invoke_map(, rcauchy(100))
#> $m1 #> [1] -0.634837 #> #> $m2 #> [1] -0.1482912 #>
# If you have pairs of function name and arguments, it's natural # to store them in a data frame. Here we use a tibble because # it has better support for list-columns df <- tibble::tibble( f = c("runif", "rpois", "rnorm"), params = list( list(n = 10), list(n = 5, lambda = 10), list(n = 10, mean = -3, sd = 10) ) ) df
#> # A tibble: 3 × 2 #> f params #> <chr> <list> #> 1 runif <list [1]> #> 2 rpois <list [2]> #> 3 rnorm <list [3]>
invoke_map(df$f, df$params)
#> [[1]] #> [1] 0.204329063 0.471755611 0.385183100 0.392751929 0.218338430 0.007208941 #> [7] 0.363712143 0.526106730 0.844163393 0.135991171 #> #> [[2]] #> [1] 9 9 5 6 9 #> #> [[3]] #> [1] -3.240840 12.844975 -2.943244 -9.482045 15.586632 5.283032 #> [7] 8.990353 3.000512 -2.412399 -15.897635 #>