[Rd] The ... argument
Mon, 09 Jul 2001 15:06:41 -0400
Deepayan Sarkar wrote:
> I'm defining the following functions:
> > foo1 <- function(...) print(match.call())
> > foo2 <- function(...) foo1(...)
> My understanding is that foo2 defined as above should
> behave exactly like foo1 (and also that this sort of
> usage is quite common). However, what I'm getting is:
> > foo1(a = F, b = list()); foo2(a = F, b = list())
> foo1(a = F, b = list())
> foo1(a = ..1, b = ..2)
> However, if the values of the arguments are constants,
> there appears to be no problem, e.g.:
> > temp <- 10
> > foo1(a = temp, b = 10, ab = FALSE); foo2(a = temp,
> + b = 10, ab = FALSE)
> foo1(a = temp, b = 10, ab = FALSE)
> foo1(a = ..1, b = 10, ab = FALSE)
> Any explanations/suggestions ?
Well, "exactly like" is overstating it a bit.
You should get the same effect in terms of the computed results.
But the evaluator has to ensure that the usual rules of evaluation
apply. In particular, that arguments only get evaluated when needed,
and (rather important!) only once per argument.
The semantics that does this is to imagine that variables ..1, ..2, etc.
are created in the environment of foo1 corresponding to the actual
arguments that matched ...
These arguments are what actually get passed around, ensuring that the
usual rules for evaluation are followed. Which also explains why the
pseudo-variables aren't needed if the actual arguments are constants.
As for suggestions, you haven't said what you REALLY wanted to do, but
it sounds like you may want to do some substituting inside foo2.
By the way, this is not R-specific behavior. It really follows from the
S language semantics, and S-Plus behaves the same way.
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: email@example.com
John M. Chambers firstname.lastname@example.org
Bell Labs, Lucent Technologies office: (908)582-2681
700 Mountain Avenue, Room 2C-282 fax: (908)582-3340
Murray Hill, NJ 07974 web: http://www.cs.bell-labs.com/~jmc
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: email@example.com