Leeds, Mark (IED)
2007-Sep-04 19:51 UTC
[R] Confusion using "functions to access the function call stack" example section
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) 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 ? 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. 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. gg <- function(y) { ggg <- function() { cat("y = ", y, "\n") 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() } gg(3) # OUTPUT y = 0 current frame is 8 parents are 0 1 2 0 4 5 6 7 function() { cat("y = ", y, "\n") cat("current frame is ", sys.nframe(), "\n") cat("parents are ", sys.parents(), "\n") print(sys.function(0)) # ggg print(sys.function(2)) # gg } <environment: 0x8a9cc68> function (expr, envir = parent.frame(), enclos = if (is.list(envir) || is.pairlist(envir)) parent.frame() else baseenv()) .Internal(eval.with.vis(expr, envir, enclos)) <environment: 0x8974ea0> -------------------------------------------------------- This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
jim holtman
2007-Sep-04 20:27 UTC
[R] Confusion using "functions to access the function call stack" example section
It is because you have a recursive function call and the value of 'y' when you print is it 0. I have added another statement that might help clarify what you are seeing. At the point at which the most current value of the function 'ggg' is evaluated (last call), the value of 'y' is zero and you are 5 levels down from the 'main frame':> gg <- function(y) {+ cat ("gg y=", y, "current frame =", sys.nframe(), "\n") + ggg <- function() { + cat("y = ", y, "\n") + 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() + }> > gg(3)gg y= 3 current frame = 1 gg y= 2 current frame = 2 gg y= 1 current frame = 3 gg y= 0 current frame = 4 y = 0 current frame is 5 parents are 0 1 2 3 4 function() { cat("y = ", y, "\n") cat("current frame is ", sys.nframe(), "\n") cat("parents are ", sys.parents(), "\n") print(sys.function(0)) # ggg print(sys.function(2)) # gg } <environment: 0x01cf5f6c> function(y) { cat ("gg y=", y, "current frame =", sys.nframe(), "\n") ggg <- function() { cat("y = ", y, "\n") 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() } On 9/4/07, Leeds, Mark (IED) <Mark.Leeds at morganstanley.com> 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) > > 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 ? > > 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. > > 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. > > > > > gg <- function(y) { > ggg <- function() { > cat("y = ", y, "\n") > 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() > } > > gg(3) > > > > # OUTPUT > > > y = 0 > current frame is 8 > parents are 0 1 2 0 4 5 6 7 > function() { > cat("y = ", y, "\n") > cat("current frame is ", sys.nframe(), "\n") > cat("parents are ", sys.parents(), "\n") > print(sys.function(0)) # ggg > print(sys.function(2)) # gg > } > <environment: 0x8a9cc68> > function (expr, envir = parent.frame(), enclos = if (is.list(envir) || > is.pairlist(envir)) parent.frame() else baseenv()) > .Internal(eval.with.vis(expr, envir, enclos)) > <environment: 0x8974ea0> > -------------------------------------------------------- > > This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Peter Dalgaard
2007-Sep-04 21:02 UTC
[R] Confusion using "functions to access the function call stack" example section
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