Learn R Programming

bazar (version 1.0.11)

as.fun: 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, ...)

# S3 method for default as.fun(x, envir = parent.frame(), ...)

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

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

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

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

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

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

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

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

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

# S3 method for 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.

Examples

Run this code
# NOT RUN {
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

# }

Run the code above in your browser using DataLab