as.fun {bazar}R Documentation

Convert object to function

Description

as.fun is a generic function that does the same as as.function from package base, with the additional feature that as.fun.character converts a string into the function it names.

Usage

as.fun(x, ...)

## Default S3 method:
as.fun(x, envir = parent.frame(), ...)

## S3 method for class 'character'
as.fun(x, ...)

## S3 method for class 'name'
as.fun(x, ...)

## S3 method for class 'call'
as.fun(x, ...)

## S3 method for class 'numeric'
as.fun(x, ...)

## S3 method for class 'logical'
as.fun(x, ...)

## S3 method for class 'factor'
as.fun(x, ...)

## S3 method for class 'complex'
as.fun(x, ...)

## S3 method for class 'data.frame'
as.fun(x, ...)

## S3 method for class 'lm'
as.fun(x, ...)

## S3 method for class 'rpart'
as.fun(x, ...)

Arguments

x

The object to convert.

...

Additional arguments (currently not used).

envir

Environment in which the function should be defined.

Value

The desired function.

Author(s)

as.fun.character is adapted from MrFlick, see https://stackoverflow.com/a/38984214 on StackOverflow.

Examples

as.fun(mean)
as.fun("mean")
as.fun("edit")
as.fun("stats::predict")

## the constant function '1'
f <- as.fun(1)
f(2)   # 1
f("a") # 1

## the constant function 'FALSE'
f <- as.fun(FALSE)
f(2)   # FALSE
f("a") # FALSE

f <- as.fun(data.frame(x = 1:2, y = 2:3))
f("x") # 'x' column
f("y") # 'y' column


[Package bazar version 1.0.11 Index]