[R] Confusion using "functions to access the function call stack" example section
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Tue Sep 4 23:02:03 CEST 2007
Leeds, Mark (IED) wrote:
> I was going through the example below which is taken from the example
> section in the R documentation for accessing the function call stack.
> I am confused and I have 3 questions that I was hoping someone could
> answer.
>
> 1) why is y equal to zero even though the call was done with gg(3)
>
There are multiple nested calls to gg, and y is counted down. You're not
calling ggg when y > 0, and that what does the printing.
> 2) what does parents are 0,1,2,0,4,5,6,7 mean ? I understand what a
> parent frame is but how do the #'s relate to this
> particular example ? Why is the current frame # 8 ?
>
How did you get that?? Did you miss the part where it said that the
example gives different results when run by example()? I get
> gg(3)
current frame is 5
parents are 0 1 2 3 4
function() {
cat("current frame is", sys.nframe(), "\n")
cat("parents are", sys.parents(), "\n")
print(sys.function(0)) # ggg
print(sys.function(2)) # gg
}
<environment: 0x8bb8e10>
function(y) {
ggg <- function() {
cat("current frame is", sys.nframe(), "\n")
cat("parents are", sys.parents(), "\n")
print(sys.function(0)) # ggg
print(sys.function(2)) # gg
}
if(y > 0) gg(y-1) else ggg()
}
which should make somewhat better sense. (My versions, 2.5.1 and
pre-2.6.0 don't seem to print y either?) As a general matter, frames
make a tree: two of them can have the same parent - e.g., this happens
whenever an argument expression is being evaluated as part of evaluating
a function call. Try, e.g.
f <- function(x) {x;print(sys.status())} ; f(f(1))
> 3) it says that sys.function(2) should be gg but I would think that
> sys.function(1) would be gg since it's one up from where
> the call is being made.
>
>
There are multiple calls to gg() so both could be true.
> Thanks a lot. If the answers are too complicated and someone knows of a
> good reference that goes into more details about
> the sys functions, that's appreciated also.
>
The best way is to just poke around with some simple examples until you
get the hang of it. Possibly modify the examples you have already seen
but print the entire sys.status().
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list