[R] Confusion using "functions to access the function call stack" example section

jim holtman jholtman at gmail.com
Tue Sep 4 22:27:27 CEST 2007


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?



More information about the R-help mailing list