R-alpha: lazy evaluation and plot.step()

Peter Dalgaard BSA p.dalgaard@kubism.ku.dk
22 Apr 1997 17:18:46 +0200


The explanation seems to be in different behaviour of the
substitute(...) construction:

R:
> f<-function(a,b){a<-1; print(deparse(substitute(c(a,b))))}
> f(a,b)
[1] "c(1, b)"

Splus:
> f<-function(a,b){a<-1; print(deparse(substitute(c(a,b))))}
> f(a,b)
[1] "c(a, b)"
[1] "c(a, b)"

So in R, assignment to a formal parameter causes the changed value to
be used for substitution purposes.

In S, this does not happen *unless the formal parameter is a
constant.*!

> f(2,b)
[1] "c(1, b)"
[1] "c(1, b)"
> f(2:2,b)
[1] "c(2:2, b)"
[1] "c(2:2, b)"
> f("a",b)    
[1] "c(1, b)"
[1] "c(1, b)"

This is confusing in both languages, but I'd say that S has the bigger
problem...

In Kurts example, inserting 

xlab 
ylab

as the first two lines of the function defeats lazy evaluation and
provides the desired behaviour (I suppose) in both R and S.

	- Peter

----

Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> writes:

> "test" <-
> function(x, y, xlab = deparse(substitute(x)), ylab = deparse(substitute(y)))
> {
>   x <- sort(x)
>   print(xlab)
>   n <- length(x)
>   y <- (1:n) / n
>   print(ylab)
> }
> 
> Under R,
> 
> R> test(4:1)
> [1] "c(1, 2, 3, 4)"
> [1] "c(0.25, 0.5, 0.75, 1)"
> R> test(4:1, 4)
> [1] "c(1, 2, 3, 4)"
> [1] "c(0.25, 0.5, 0.75, 1)"
> 
> Under S,
> 
> > test(4:1)
> [1] "4:1"
> [1] ""
> [1] ""
> > test(4:1, 4)
> [1] "4:1"
> [1] "c(0.25, 0.5, 0.75, 1)"
> [1] "c(0.25, 0.5, 0.75, 1)"
> 
> ???


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel 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-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-