as_function {rlang} | R Documentation |
Convert to function
Description
as_function()
transforms a one-sided formula into a function.
This powers the lambda syntax in packages like purrr.
Usage
as_function(
x,
env = global_env(),
...,
arg = caller_arg(x),
call = caller_env()
)
is_lambda(x)
Arguments
x |
A function or formula. If a function, it is used as is. If a formula, e.g. If a string, the function is looked up in |
env |
Environment in which to fetch the function in case |
... |
These dots are for future extensions and must be empty. |
arg |
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem. |
call |
The execution environment of a currently
running function, e.g. |
Examples
f <- as_function(~ .x + 1)
f(10)
g <- as_function(~ -1 * .)
g(4)
h <- as_function(~ .x - .y)
h(6, 3)
# Functions created from a formula have a special class:
is_lambda(f)
is_lambda(as_function(function() "foo"))