EVOLUTION-MANAGER
Edit File: invoke.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Invoke functions.</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for invoke {purrr}"><tr><td>invoke {purrr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Invoke functions.</h2> <h3>Description</h3> <a href='https://www.tidyverse.org/lifecycle/#retired'><img src='figures/lifecycle-retired.svg' alt='Retired lifecycle'></a> <p>This pair of functions make it easier to combine a function and list of parameters to get a result. <code>invoke</code> is a wrapper around <code>do.call</code> that makes it easy to use in a pipe. <code>invoke_map</code> makes it easier to call lists of functions with lists of parameters. </p> <h3>Usage</h3> <pre> 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) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.f</code></td> <td> <p>For <code>invoke</code>, a function; for <code>invoke_map</code> a list of functions.</p> </td></tr> <tr valign="top"><td><code>.x</code></td> <td> <p>For <code>invoke</code>, an argument-list; for <code>invoke_map</code> a list of argument-lists the same length as <code>.f</code> (or length 1). The default argument, <code>list(NULL)</code>, will be recycled to the same length as <code>.f</code>, and will call each function with no arguments (apart from any supplied in <code>...</code>.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments passed to each function.</p> </td></tr> <tr valign="top"><td><code>.env</code></td> <td> <p>Environment in which <code><a href="../../base/html/do.call.html">do.call()</a></code> should evaluate a constructed expression. This only matters if you pass as <code>.f</code> the name of a function rather than its value, or as <code>.x</code> symbols of objects rather than their values.</p> </td></tr> </table> <h3>Life cycle</h3> <p>These functions are retired in favour of <code><a href="exec.html">exec()</a></code>. They are no longer under active development but we will maintain them in the package undefinitely. </p> <ul> <li> <p><code>invoke()</code> is retired in favour of the simpler <code>exec()</code> function reexported from rlang. <code>exec()</code> evaluates a function call built from its inputs and supports tidy dots:</p> <pre># Before: invoke(mean, list(na.rm = TRUE), x = 1:10) # After exec(mean, 1:10, !!!list(na.rm = TRUE)) </pre> </li> <li> <p><code>invoke_map()</code> is is retired without replacement because it is more complex to understand than the corresponding code using <code>map()</code>, <code>map2()</code> and <code>exec()</code>:</p> <pre># 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)) </pre> </li></ul> <h3>See Also</h3> <p>Other map variants: <code><a href="imap.html">imap</a>()</code>, <code><a href="lmap.html">lmap</a>()</code>, <code><a href="map2.html">map2</a>()</code>, <code><a href="map_if.html">map_if</a>()</code>, <code><a href="map.html">map</a>()</code>, <code><a href="modify.html">modify</a>()</code> </p> <h3>Examples</h3> <pre> # Invoke a function with a list of arguments invoke(runif, list(n = 10)) # Invoke a function with named arguments invoke(runif, n = 10) # Combine the two: invoke(paste, list("01a", "01b"), sep = "-") # That's more natural as part of a pipeline: list("01a", "01b") %>% invoke(paste, ., sep = "-") # Invoke a list of functions, each with different arguments invoke_map(list(runif, rnorm), list(list(n = 10), list(n = 5))) # Or with the same inputs: invoke_map(list(runif, rnorm), list(list(n = 5))) invoke_map(list(runif, rnorm), n = 5) # Or the same function with different inputs: invoke_map("runif", list(list(n = 5), list(n = 10))) # Or as a pipeline list(m1 = mean, m2 = median) %>% invoke_map(x = rcauchy(100)) list(m1 = mean, m2 = median) %>% invoke_map_dbl(x = rcauchy(100)) # 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)) # 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) } </pre> <hr /><div style="text-align: center;">[Package <em>purrr</em> version 0.3.4 <a href="00Index.html">Index</a>]</div> </body></html>