Skip to content
Prev 205840 / 398506 Next

Arguments of a function

Normally one designs their function to input a formula in such a case
rather than design it to take the names directly.  Thus:

f <- function(formula = ~ x1 + x2) {
  xs <- c(x1 = 1, x2 = exp(1), x3 = 2*pi)
  v <- all.vars(formula)
  stopifnot(length(v) == 2, all(v %in% names(xs)))
  sum(xs[v])
}

# test
f()
f(~ x1 + x2) # same
f(~ x2+x3)
On Fri, Jan 8, 2010 at 1:15 PM, Lisa <lisajca at gmail.com> wrote: