as.fun: Convert object to function

Description Usage Arguments Value Author(s) Examples

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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

bazar documentation built on May 2, 2019, 7:02 a.m.