Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

Re: [R] Evaluation of defaults in functions

Deepayan Sarkar

2006-09-28

Replies:

On 9/28/06, Ulrich Keller <uhkeller@(protected):
> 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

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.