Message-ID: <971536df1001090418o3ecd0ecav8910b1b4231566de@mail.gmail.com>
Date: 2010-01-09T12:18:56Z
From: Gabor Grothendieck
Subject: Arguments of a function
In-Reply-To: <1262974513137-1009883.post@n4.nabble.com>
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:
>
> Dear all,
>
> I have a question about how to set arguments in my own function. For
> example, I have a function that looks like this:
>
> my.f <- function(a = x1, b = x2)
> {
> ? x1 = equation 1
> ? x2 = equation 2
> ? x3 = equation 3
> ? y = a + b
> }
>
> x1, x2, and x3 are temporary variables (intermediate results) calculated
> from other variables within the funciton. I want to use two of these three
> variables to calculate y, and write R script as below:
>
> my.f(a = x1, b = x2)
>
> or
>
> my.f(a = x2, b = x3)
>
> The error information shows that: ?objects 'x1', 'x2', or 'x3' not found?.
>
> Can anybody help me solve this problem? Thanks in advance.
>
> Lisa
>
> --
> View this message in context: http://n4.nabble.com/Arguments-of-a-function-tp1009883p1009883.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>