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

2007-09-04 Thread Leeds, Mark \(IED\)
thanks jim : what you said makes a lot of sense so I changed mine to
what you did and I get similar output but everything looks
shifted up by 3 frames. That probably has to do with me running on a
different os ( linux ) or R.2.5.0 rather than R.2.5.1, I guess ? 
It makes a lot more sense now so that's fine.




source("frame.R")
gg y= 3 current frame = 4 
gg y= 2 current frame = 5 
gg y= 1 current frame = 6 
gg y= 0 current frame = 7 
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
}
 





-Original Message-
From: jim holtman [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 04, 2007 4:27 PM
To: Leeds, Mark (IED)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [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
   }

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) <[EMAIL PROTECTED]> 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
>}
> 
> function (expr, envir = parent.frame(), enclos = if (is.list(envir) ||
>

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

2007-09-04 Thread Peter Dalgaard
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
}

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
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
R-help@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.


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

2007-09-04 Thread jim holtman
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
   }

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) <[EMAIL PROTECTED]> 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
>}
> 
> function (expr, envir = parent.frame(), enclos = if (is.list(envir) ||
>is.pairlist(envir)) parent.frame() else baseenv())
> .Internal(eval.with.vis(expr, envir, enclos))
> 
> 
>
> This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
>
> __
> R-help@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?

__
R-help@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.


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

2007-09-04 Thread Leeds, Mark \(IED\)
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
}

function (expr, envir = parent.frame(), enclos = if (is.list(envir) || 
is.pairlist(envir)) parent.frame() else baseenv()) 
.Internal(eval.with.vis(expr, envir, enclos))



This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}

__
R-help@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.