as.function.formula {gsubfn}R Documentation

Make a one-line function from a formula.

Description

Create a function from a formula.

Usage

## S3 method for class 'formula':
as.function(x, ...)

Arguments

x Formula with no left side.
... Currently not used.

Value

A function is returned whose formal arguments are the variables in the left hand side, whose body is the expression on the right side of the formula and whose environment is the environment of the formula. If there is no left hand side the free variables on the right, in the order encountered are used as the arguments. letters, LETTERS and pi are ignored and not used as arguments. If the left hand side is 0 then the function is created as a zero argument function.

Note

->, ->>, =, <-, <<- and ? all have lower operator precdence than ~ so function bodies that contain them typically must be surrounded with {...}.

See Also

Syntax.

Examples

as.function(~ as.numeric(x) + as.numeric(y))
as.function(x + y ~ as.numeric(x) + as.numeric(y)) # same
## Not run: 
# example where function body must be surrounded with {...} 
# due to use of <<-.  See warning section above.
assign("mywarn", NULL, .GlobalEnv)
fn$tryCatch( warning("a warning"), 
    warning = w ~ { mywarn <<- conditionMessage(w)})
print(mywarn)
## End(Not run)

[Package gsubfn version 0.3-8 Index]