as_function

0th

Percentile

Convert to function or closure

• as_function() transform objects to functions. It fetches functions by name if supplied a string or transforms quosures to a proper function.

• as_closure() first passes its argument to as_function(). If the result is a primitive function, it regularises it to a proper closure (see is_function() about primitive functions).

Usage
as_function(x, env = caller_env())as_closure(x, env = caller_env())
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 two arguments, .x or . and .y. This allows you to create very compact anonymous functions with up to two inputs.

env

Environment in which to fetch the function in case x is a string.

• as_function
• as_closure
Examples
# NOT RUN {
f <- as_function(~ . + 1)
f(10)

# Primitive functions are regularised as closures
as_closure(list)
as_closure("list")

# Operators have .x and .y as arguments, just like lambda
# functions created with the formula syntax:
as_closure(+)
as_closure(~)
# }

Documentation reproduced from package rlang, version 0.1.6, License: GPL-3

Community examples

Looks like there are no examples yet.