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:> 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: 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? Kjetil Halvorsen -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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: > > > 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: > > > 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?You example is a call, and your usage is a formal argument. Yours sets the default for `x' to x, which does not exist. lm's calls lm.fit with named argument `offset'.> > Kjetil Halvorsen > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
The way I look at this, perhaps naively, is this: > test <- function(x=x) x*x is defining a function, whereas lm.fit(x, y, offset = offset, ...) is calling a function. The syntax may look the same, but the operation is different. lm.fit is defined > args(lm.fit) function (x, y, offset = NULL, method = "qr", tol = 1e-07, ...) so it is not applicable as a counter-example. -Don At 11:46 AM -0400 6/26/02, 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: > >> 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: > > > 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? > >Kjetil Halvorsen >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- >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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA -------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._