[R] evaluation revisited

markleeds at verizon.net markleeds at verizon.net
Wed Jan 28 08:29:01 CET 2009


I'm still going over old emails and trying to get my head around 
evaluation so I'm persistent if nothing else.

  A while back , an expert sent me below as an exercise in understanding 
and I only got around to it tonight. I understand some of the output but 
not all of it and I put "Why not Zero ?" next to the ones that I don't 
understand based on my reading of the various functions in the help 
pages. It's either my reading comprehension or the evaluation subtleties 
in R but I just can't understand some of them. If any of the expeRts has 
time to explain the ones that I marked with "WHY NOT ZERO ?", it would 
be much appreciated. Obviously, I don't expect a long explanation but I 
think my problem is that I keep thinking that eval.parent and 
eval(whatever, parent.frame) go back to the function that called 
with.options so f() and do the evaluation in there but that doesn't 
always seem to be the case.  I'm also not so clear on the difference 
between print(x) and L[[len]]. Thanks a lot in advance to anyone who can 
be bothered with below.

with.options <- function(...) {
   L <- as.list(match.call())[-1]
   len <- length(L)
   print(L)

   eval.parent(L[[len]])  # =0 MAKES SENSE
   eval(L[[len]]) # =1  MAKES SENSE
   eval(L[[len]],parent.frame()) # =0 MAKES SENSE
   eval.parent(print(x)) # =1   WHY NOT ZERO ?   Somehow this is 
different from eval.parent(L[[len]])
   eval(print(x)) # =1   MAKES SENSE
   eval(print(x),parent.frame()) # =1 # WHY NOT ZERO ? Somehow this is 
different from eval(L[[len]],parent.frame)
   evalq(print(x)) # =1 MAKES SENSE
   evalq(print(x),parent.frame()) # =1 MAKES SENSE
   print("====================")

   x <- 2

   eval.parent(L[[len]]) # =0  MAKES SENSE
   eval(L[[len]]) # =2  MAKES SENSE
   eval(L[[len]],parent.frame()) # =0  MAKES SENSE
   eval.parent(print(x)) # =2  WHY NOT ZERO ?  Somehow this is different 
from eval.parent(L[[len]])
   eval(print(x))  # 2  MAKES SENSE
   eval(print(x),parent.frame()) # 2 WHY NOT ZERO ? Somehow this is 
different from eval(L[[len]], parent.frame)
   evalq(print(x))  # 2   MAKES SENSE
   evalq(print(x),parent.frame()) # 1 WHY NOT ZERO ?
   print("====================")

}

x <- 1

f <- function() {
   x <- 0
   with.options(width = 40, print(x))
}

f()




More information about the R-help mailing list