Hello, and sorry if this is already explained somewhere. I couldn't find anything. R (2.3.1, Windows) seems to perform some kind of lazy evaluation when evaluating defaults in function calls that, at least for me, leads to unexpected results. Consider the following, seemingly equivalent functions: > foo1 <- function(x, y=x) { + x <- 0 + y + } > foo1(1) [1] 0 > foo2 <- function(x, y=x) { + y <- y + x <- 0 + y + } > foo2(1) [1] 1 Obviously, y is not evaluated until it is used in some way. I would expect it to be evaluated where it is defined. Is this intended behavior? Thanks for clarifying, Uli
On 9/28/06, Ulrich Keller <uhkeller at web.de> wrote:> Hello, > > and sorry if this is already explained somewhere. I couldn't find anything. > > R (2.3.1, Windows) seems to perform some kind of lazy evaluation when > evaluating defaults in function calls that, at least for me, leads to > unexpected results. Consider the following, seemingly equivalent functions: > > > foo1 <- function(x, y=x) { > + x <- 0 > + y > + } > > foo1(1) > [1] 0 > > foo2 <- function(x, y=x) { > + y <- y > + x <- 0 > + y > + } > > foo2(1) > [1] 1 > > Obviously, y is not evaluated until it is used in some way. I would > expect it to be evaluated where it is defined. Is this intended behavior?Yes. For a similar example see the 'logplot' function and related discussion in http://cran.r-project.org/doc/manuals/R-lang.html#Substitutions -Deepayan
On Thu, 2006-09-28 at 21:49 +0200, Ulrich Keller wrote:> Hello, > > and sorry if this is already explained somewhere. I couldn't find anything. > > R (2.3.1, Windows) seems to perform some kind of lazy evaluation when > evaluating defaults in function calls that, at least for me, leads to > unexpected results. Consider the following, seemingly equivalent functions: > > > foo1 <- function(x, y=x) { > + x <- 0 > + y > + } > > foo1(1) > [1] 0 > > foo2 <- function(x, y=x) { > + y <- y > + x <- 0 > + y > + } > > foo2(1) > [1] 1 > > Obviously, y is not evaluated until it is used in some way. I would > expect it to be evaluated where it is defined. Is this intended behavior? > Thanks for clarifying, > > UliYep. This is documented in the R Language Definition Manual, which is available via the GUI in the Windows version and/or online here: http://cran.r-project.org/doc/manuals/R-lang.html Specifically in section 4.3.3 Argument Evaluation: "R has a form of lazy evaluation of function arguments. Arguments are not evaluated until needed. It is important to realize that in some cases the argument will never be evaluated. Thus, it is bad style to use arguments to functions to cause side-effects. While in C it is common to use the form, foo(x = y) to invoke foo with the value of y and simultaneously to assign the value of y to x this same style should not be used in R. There is no guarantee that the argument will ever be evaluated and hence the assignment may not take place." You might also want to read section 2.1.8 Promise objects and section 6.2 Substitutions. HTH, Marc Schwartz
foo2 could be written: foo3 <- function(x, y = x) { force(y); x <- 0; y } to make it clear that evaluation of y is being forced. See ?force On 9/28/06, Ulrich Keller <uhkeller at web.de> wrote:> Hello, > > and sorry if this is already explained somewhere. I couldn't find anything. > > R (2.3.1, Windows) seems to perform some kind of lazy evaluation when > evaluating defaults in function calls that, at least for me, leads to > unexpected results. Consider the following, seemingly equivalent functions: > > > foo1 <- function(x, y=x) { > + x <- 0 > + y > + } > > foo1(1) > [1] 0 > > foo2 <- function(x, y=x) { > + y <- y > + x <- 0 > + y > + } > > foo2(1) > [1] 1 > > Obviously, y is not evaluated until it is used in some way. I would > expect it to be evaluated where it is defined. Is this intended behavior? > Thanks for clarifying, > > Uli > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >