[R] Bug or failing understanding?

Thomas Lumley tlumley at u.washington.edu
Wed Jun 26 18:03:02 CEST 2002


On Wed, 26 Jun 2002, kjetil halvorsen wrote:

> Hola!
>
> I seem to remember i used to have the same name of argument and default
> value in argument list to functions, but (rw1.5.1) this seems not to
> work:

It shouldn't ever have worked.

> > x <- 3
> > test <- function(x=x) x*x
> > test(7)
> [1] 49
> > test()
> Error in test() : recursive default argument reference
>
>
> here is a code fragment from lm() using the same syntax:

But this doesn't use the same syntax. If you mean offset=offset, this  is
using an actual argument with the same name as the formal argument. That's
very different from using a *default* with the same name as the formula
argument

>
>  else {
>         x <- model.matrix(mt, mf, contrasts)
>         z <- if (is.null(w))
>             lm.fit(x, y, offset = offset, ...)
>         else lm.wfit(x, y, w, offset = offset, ...)
>
> What is wrong?

Actual arguments are looked up in the calling environment, so the
offset=offset in lm.fit uses the variable "offset" in the calling
environment.

Defaults are looking up in the local environment, so your example looks
for the local variable "x". This is just the formal argument "x", leading
around in circles.

There is a good reason that defaults are evaluated in the local
environment. It allows the default for one parameter to depend on the
value of another.

	 -thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list