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.
~ .x + 2
, it is converted to a function with up to two arguments:.x
(single argument) or.x
and.y
(two arguments). The.
placeholder can be used instead of.x
. This allows you to create very compact anonymous functions (lambdas) with up to two inputs. Functions created from formulas have a special class. Useis_lambda()
to test for it.If a string, the function is looked up in
env
. Note that this interface is strictly for user convenience because of the scoping issues involved. Package developers should avoid supplying functions by name and instead supply them by value.- env
Environment in which to fetch the function in case
x
is a string.- ...
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.
caller_env()
. The function will be mentioned in error messages as the source of the error. See thecall
argument ofabort()
for more information.