Skip to content

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

3 messages · Peter Dalgaard, Kurt Hornik

#
Today I received an email which made me realize that what I wrote in the
FAQ explaining the different behavior of Martin's plot.step() under R
and S is not really true.

Does someone understand what is going on in the following?

"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,
[1] "4:1"
[1] ""
[1] ""
[1] "4:1"
[1] "c(0.25, 0.5, 0.75, 1)"
[1] "c(0.25, 0.5, 0.75, 1)"

???

-k
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
The explanation seems to be in different behaviour of the
substitute(...) construction:

R:
[1] "c(1, b)"

Splus:
[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.*!
[1] "c(1, b)"
[1] "c(1, b)"
[1] "c(2:2, b)"
[1] "c(2:2, 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:

  
    
#
Not a surprise ...

Thanks, Peter, for the explanation.  Does this also explain the
different results for x <- sort(x)?
Yes.  This is also what I suggested to Martin in order to have a version
of plot.step() which does the same under R and S.

-k
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-