Retired lifecycle

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_raw(.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 supplied 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.

Life cycle

These functions are retired in favour of exec(). They are no longer under active development but we will maintain them in the package undefinitely.

  • invoke() is retired in favour of the simpler exec() function reexported from rlang. exec() evaluates a function call built from its inputs and supports tidy dots:

    # Before:
    invoke(mean, list(na.rm = TRUE), x = 1:10)
    
    # After
    exec(mean, 1:10, !!!list(na.rm = TRUE))
    
  • invoke_map() is is retired without replacement because it is more complex to understand than the corresponding code using map(), map2() and exec():

    # Before:
    invoke_map(fns, list(args))
    invoke_map(fns, list(args1, args2))
    
    # After:
    map(fns, exec, !!!args)
    map2(fns, list(args1, args2), function(fn, args) exec(fn, !!!args))
    

See also

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

Examples

# Invoke a function with a list of arguments invoke(runif, list(n = 10))
#> [1] 0.3277343 0.6021007 0.6043941 0.1246334 0.2946009 0.5776099 0.6309793 #> [8] 0.5120159 0.5050239 0.5340354
# Invoke a function with named arguments invoke(runif, n = 10)
#> [1] 0.55724944 0.86791949 0.82970869 0.11144915 0.70368836 0.89748826 #> [7] 0.27973255 0.22820188 0.01532989 0.12898156
# 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.09338193 0.23688501 0.79114741 0.59973157 0.91014771 0.56042455 #> [7] 0.75570477 0.37917189 0.37328098 0.17029064 #> #> [[2]] #> [1] -0.1173097 -0.4226757 -0.8346894 -0.8146651 0.7935841 #>
# Or with the same inputs: invoke_map(list(runif, rnorm), list(list(n = 5)))
#> [[1]] #> [1] 0.57074752 0.41928296 0.26762217 0.04780944 0.10349305 #> #> [[2]] #> [1] -0.48445511 -0.74107266 1.16061578 1.01206712 -0.07207847 #>
invoke_map(list(runif, rnorm), n = 5)
#> [[1]] #> [1] 0.12781466 0.27968351 0.81610606 0.05761299 0.80282925 #> #> [[2]] #> [1] -1.2569391 -0.5106143 0.1020858 -1.3251728 0.7089426 #>
# Or the same function with different inputs: invoke_map("runif", list(list(n = 5), list(n = 10)))
#> [[1]] #> [1] 0.9662828 0.5152566 0.5494807 0.1637199 0.1645970 #> #> [[2]] #> [1] 0.786332777 0.751113420 0.784214704 0.654354915 0.378104397 0.008566967 #> [7] 0.955329554 0.838616148 0.213424938 0.494713556 #>
# Or as a pipeline list(m1 = mean, m2 = median) %>% invoke_map(x = rcauchy(100))
#> $m1 #> [1] 1.375921 #> #> $m2 #> [1] 0.1906799 #>
list(m1 = mean, m2 = median) %>% invoke_map_dbl(x = rcauchy(100))
#> m1 m2 #> 0.13422742 0.05007979
# 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.8621935 #> #> $m2 #> [1] -0.06702543 #>
# 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 if (rlang::is_installed("tibble")) { 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 invoke_map(df$f, df$params) }
#> [[1]] #> [1] 0.99628168 0.33100748 0.08520752 0.85446765 0.52302976 0.99703446 #> [7] 0.42188735 0.22862927 0.10356121 0.89888084 #> #> [[2]] #> [1] 7 12 10 8 7 #> #> [[3]] #> [1] 11.611718 -19.725326 12.610960 -17.933706 -14.824491 -6.565869 #> [7] -12.156401 5.494855 -7.896072 4.279832 #>