R: Convert an object into a function.

as_function {purrr} | R Documentation

## Convert an object into a function.

### Description

`as_function` is the powerhouse behind the varied function specifications that purrr functions allow. This is an S3 generic so that other people can make `as_function` work with their own objects.

### Usage

    as_function(.f, ...)

    ## S3 method for class 'character'
    as_function(.f, ..., .null = NULL)

### Arguments

`.f` | A function, formula, or atomic vector. 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. If character or integer vector, e.g. `"y"`, it is converted to an extractor function, `function(x) x[["y"]]`. To index deeply into a nested list, use multiple values; `c("x", "y")` is equivalent to `z[["x"]][["y"]]`. You can also set `.null` to set a default to use instead of `NULL` for absent components.
`...` | Additional arguments passed on to methods.
`.null` | Optional additional argument for character and numeric inputs.

### Examples

    as_function(~ . + 1)
    as_function(1)
    as_function(c("a", "b", "c"))
    as_function(c("a", "b", "c"), .null = NA)

[Package purrr version 0.2.2 Index]