#### /man/as.function.formula.Rd

Unknown | 54 lines | 54 code | 0 blank | 0 comment | 0 complexity | 1863bf11ffc8bf378f37688240f46e18 MD5 | raw file
 1\name{as.function.formula}
2\alias{as.function.formula}
3\title{ Make a one-line function from a formula. }
4\description{
5  Create a function from a formula.
6}
7\usage{
8\method{as.function}{formula}(x, ...)
9}
10\arguments{
11  \item{x}{ Formula with no left side. }
12  \item{\dots}{ Currently not used. }
13}
14\value{
15  A function is returned whose formal arguments are the
16  variables in the left hand side, whose body
17  is the expression on the right side of the formula and whose
18  environment is the environment of the formula.  If there is
19  no left hand side the free variables on the right, in the
20  order encountered are used as the arguments.  \code{letters},
21  \code{LETTERS} and \code{pi} are ignored and not used as arguments.
22  If there is no left hand side and
23  any of \code{..1}, \code{..2}, ..., \code{..9} are found
24  as free variables then they are not used as arguments but \code{...}
25  is used as a trailing argument instead.
26  If there is no left hand side and \code{&} is found as a free variable
27  then that variable is used as the first argument and \code{...} is added
28  as the last argument.
29  If the left hand side is \code{0} then the function is created as
30  a zero argument function.
31}
32\note{\code{->}, \code{->>}, \code{=}, \code{<-}, \code{<<-} and \code{?}
33all have lower operator precdence than \code{~} so function bodies that
34contain them typically must be surrounded with \code{{...}}.}
36\examples{
37old.options <- options(keep.source = FALSE)
38as.function(~ as.numeric(x) + as.numeric(y))
39as.function(x + y ~ as.numeric(x) + as.numeric(y)) # same
40as.function(~ ..1 + ..2)
41# the replacement function in gsubfn uses as.function.formula to
42# interpret formulas as functions.  Here we insert ! after each digit.
43gsubfn("[0-9]", ~ paste0(&, "!"), "ab4cd5")
44\dontrun{
45# example where function body must be surrounded with {...}
46# due to use of <<-.  See warning section above.
47assign("mywarn", NULL, .GlobalEnv)
48fn\$tryCatch( warning("a warning"),
49    warning = w ~ { mywarn <<- conditionMessage(w)})
50print(mywarn)
51}
52options(old.options)
53}
54\keyword{ character }