[R] strange things with eval and parent frames

Mark.Bravington@csiro.au Mark.Bravington at csiro.au
Wed Sep 4 11:35:11 CEST 2002


Thanks for the very quick and helpful explanation, Peter (and to Martin
Maechler for telling me about identical() ).

I'm still puzzled by the sys.call case. If I fake the issue by going one
level deeper, and then calling sys.call( sys.parent()) instead of
sys.call(), I do get what I expected to get (i.e. the call to the top-level
function). The example is:

test.parent.funcs3_ function() {
  subfunc3_ function() sys.call( -1) 
  x_ subfunc3(); print( x) # the top-level call
  
  subfunc3a_ function() eval( quote( sys.call()), parent.frame())
  x_ subfunc3a(); print( x) # I wanted the top level call, but the "eval"
gets intercepted
  
  subfunc3c_ function() eval( quote( { function() sys.call( sys.parent())
}() ), parent.frame())
  x_ subfunc3c(); print( x) # the top level call
}

> test.parent.funcs3()
test.parent.funcs3()
eval(expr, envir, enclos)
test.parent.funcs3()

The 3rd output line, which creates a temporary function and calls it,
"works" or at any rate is different from subfunc3a. The behaviour of
sys.call still seems inconsistent to me: if there's no way to pretend that
eval didn't happen, as you say, then isn't it a bug that you can fool the
system by going down a level and then up again? Actually I'd rather not have
the "fool" "bug" fixed, because it does give me a workround :), as follows:

my.sys.call_ function(which=0) {
  if( which==0)
    sys.call( sys.parent())
  else if( which<0)
    sys.call( which-1)
  else
    sys.call( which)
}

Note also:

test.parent.funcs4()_ function( ...) {
  subfunc1_ function() eval( quote( match.call()), envir=parent.frame())
  x_ subfunc1(); print( x)
}

> test.parent.funcs4()
test.parent.funcs4()

so that match.call works differently from sys.call here (it "ignores" the
call to eval).

cheers
Mark 

-----Original Message-----
From: Peter Dalgaard BSA
To: Mark.Bravington at csiro.au
Cc: r-help at stat.math.ethz.ch
Sent: 4/09/2002 4:14 PM
Subject: Re: [R] strange things with eval and parent frames

Mark.Bravington at csiro.au writes:

> Dear mailing list,
> 
> I have found some strange behaviour which I think relates to parent
frames
> and eval. Can anyone explain what's going on here? 
> 
<< deleted stuff >> 

> Second example:
> > test.parent.funcs2_ function() {
>   subfunc3_ function() sys.call( -1)
>   x_ subfunc3(); print( x)
>   
>   subfunc3a_ function() eval( quote( sys.call()), parent.frame())
>   x_ subfunc3a(); print( x)
>   
>   subfunc3b_ function() evalq( sys.call(), parent.frame())
>   x_ subfunc3b(); print( x)
> }
> 
> > test.parent.funcs2()
> test.parent.funcs2()
> eval(expr, envir, enclos)
> eval(expr, envir, enclos)
> 
> Hmmm. Again, I'd expect all three to do the same thing (have I
> misunderstood?). What's even more odd, is that this time evalq and
eval(
> quote()) do coincide, but neither of them gives the answer I expected.
I
> thought that by doing the evaulation in the parent.frame, I could
knock 1
> level off the number of generations to go back in sys.call.

This one is not a bug. There's just no way that you can make the
system pretend that the call to eval() or evalq() didn't happen. (And
sys.call() is independent of evaluation environment, so the
parent.frame() anomaly doesn't matter).

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



More information about the R-help mailing list