[R] strange things with eval and parent frames

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Sep 4 16:17:29 CEST 2002


Mark.Bravington at csiro.au writes:

> 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()

This stuff is fairly weird, and I definitely got something wrong:
parents are not the same as the "return addresses", as the examples in
help(sys.parents()) clearly show. (You might want to stick in a couple
of print(sys.status()) into your code to see what the system believes
that its state is).

There might in fact be a bug -- the logic of R_sysparent (in
context.c) does not look very robust against the case where several
elements on the context stack have the same cptr->cloenv (you
basically get the lowest numbered one in that case as far as I can
see). Or, then again, perhaps that is not a bug...; Robert Gentleman
is the expert on these matters.

(I'm not quite clear-headed enough today, but I think your 3c example
amounts to finding "the parent of my first incarnation" instead of "my
parent" by using the fact that "my first incarnation" is the parent of
my child....) 


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