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

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Tue Sep 4 21:51:17 CEST 2007


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



More information about the R-help mailing list