/man/as.function.formula.Rd

http://gsubfn.googlecode.com/ · 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{?}
  33. all have lower operator precdence than \code{~} so function bodies that
  34. contain them typically must be surrounded with \code{{...}}.}
  35. \seealso{ \code{\link[base]{Syntax}}.}
  36. \examples{
  37. old.options <- options(keep.source = FALSE)
  38. as.function(~ as.numeric(x) + as.numeric(y))
  39. as.function(x + y ~ as.numeric(x) + as.numeric(y)) # same
  40. as.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.
  43. gsubfn("[0-9]", ~ paste0(`&`, "!"), "ab4cd5")
  44. \dontrun{
  45. # example where function body must be surrounded with {...}
  46. # due to use of <<-. See warning section above.
  47. assign("mywarn", NULL, .GlobalEnv)
  48. fn$tryCatch( warning("a warning"),
  49. warning = w ~ { mywarn <<- conditionMessage(w)})
  50. print(mywarn)
  51. }
  52. options(old.options)
  53. }
  54. \keyword{ character }