Hi, It seems that a formal function argument can not default to an "outer" variable of the same name:> x <- "foo" > ff <- function(x=x) x > ff()Error in ff() : recursive default argument reference>Is this intentional? Why? I use R-1.9.1. Thanks, Vadim
> From: Vadim Ogranovich > > Hi, > > It seems that a formal function argument can not default to an "outer" > variable of the same name: > > > x <- "foo"This is just a red herring... Even w/o this you get the same thing:> ff <- function(x=x) x > ff()Error in ff() : recursive default argument reference> ff(x)Error in ff(x) : Object "x" not found Note how the two errors are different.> > ff <- function(x=x) x > > ff() > Error in ff() : recursive default argument reference > > > > Is this intentional? Why?It should be, because R doesn't know what to make of it (and neither do I). Cheers, Andy> I use R-1.9.1. > > Thanks, > Vadim > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >
Vadim Ogranovich <vograno <at> evafunds.com> writes: : : Hi, : : It seems that a formal function argument can not default to an "outer" : variable of the same name: : : > x <- "foo" : > ff <- function(x=x) x : > ff() : Error in ff() : recursive default argument reference : > : : Is this intentional? Why? : Because then one arg could not safely refer to another. Here x has y as its default value: x <- 10 y <- 7 f <- function(x = y, y = 3) print(x) f() # prints 3 Note that you could do this: x <- "foo" ff <- function(x.=x) x. ff() # returns "foo" ff(x=44) # returns 44 ff(55) # returns 55 For a real example look at merge.data.frame.
Got it! Thanks to everyone for the help! Vadim> -----Original Message----- > From: r-devel-bounces@stat.math.ethz.ch > [mailto:r-devel-bounces@stat.math.ethz.ch] On Behalf Of Gabor > Grothendieck > Sent: Tuesday, November 09, 2004 5:53 PM > To: r-devel@stat.math.ethz.ch > Subject: Re: [Rd] recursive default argument reference > > Vadim Ogranovich <vograno <at> evafunds.com> writes: > > : > : Hi, > : > : It seems that a formal function argument can not default to > an "outer" > : variable of the same name: > : > : > x <- "foo" > : > ff <- function(x=x) x > : > ff() > : Error in ff() : recursive default argument reference > : > > : > : Is this intentional? Why? > : > > Because then one arg could not safely refer to another. Here > x has y as its default value: > > x <- 10 > y <- 7 > f <- function(x = y, y = 3) print(x) > f() # prints 3 > > Note that you could do this: > > x <- "foo" > ff <- function(x.=x) x. > ff() # returns "foo" > ff(x=44) # returns 44 > ff(55) # returns 55 > > For a real example look at merge.data.frame. > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >