# Re: [R] a question about frame

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 16 Jun 2005 - 09:34:10 EST

On 6/15/05, ronggui <0034058@fudan.edu.cn> wrote:
> gg <- 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()
> }
>
>
> > 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: 071C62FC>
> 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()
> }
>
>
> my question is ,why the current frame is 5?why the sys.function is ggg.i am quite confused.
> anyone can give me some clue?i have read the ?sys.parent,but i still can not get the point exactly.

We start out at frame 0 and then:

```1. we call gg(3) so now we are at frame 1
2. which calls gg(2) so now we are at frame 2
3. which calls gg(1) so now we are at frame 3
4. which calls gg(0) so now we are at frame 4
5. which calls ggg() so now we are at frame 5

```

sys.function(0) seems to be referring to the current frame (not frame 0 as the which= documentation in ?sys.function seems to imply). Since sys.function(0) is called within ggg we get the result. Also sys.function(2) refers to frame 2 and referring to the numbered list above we see it is gg.

R-help@stat.math.ethz.ch mailing list