[R] Tcl/tk , question about the environment of the call

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed May 29 20:40:09 CEST 2002


jw9 at softhome.net writes:

> This is my simple code, my intention is to let the inner function
> browse the objects in the outer function. ####################### tk1
> <- function() {
> 	tk2 <- function() {
> 		inner <- 3
> 		list2 <- ls()
> 		#list objects in fuction tk2
> 		print("**** inner objects ****")
> 		print(list2)
> 		
> 		#list objects in fuction tk1
> 		list_parent <- ls(parent.frame())
> 		print("**** outer objects ****")
> 		print(list_parent)
> 	}
> 	tt <- tktoplevel()
> 	outer <- 1
> 	tkpack(tkbutton(tt,text="push",command=tk2()))
> 	tk2()
> }
> ####################### there's a call of tk2 inside tk1, so when I
> run tk1(), it gives the output I expected: [1] "**** inner objects
> ****"
> [1] "inner"
> [1] "**** outer objects ****"
> [1] "outer" "tk2"   "tt" but when I clicked the push button, which
> also bind to the tk2 function, the result is different from that from
> the first tk2() call:
> > [1] "**** inner objects ****"
> [1] "inner"
> [1] "**** outer objects ****"
> character(0) I am not sure in which part I made the mistake,  Any help
> or suggestions will be highly appreciated. Thanks!

You mean command=tk2, not tk2(), I assume (please test code before
submitting, and avoid putting Tab characters inside).

However, this is not really a Tcl/Tk issue. If you had assigned tk2 to
a global variable, you would have seen the same effect when executing
it. The parent of a call is not necessarily the enclosure (aka the
"parent environment"). parent.frame() is not the local frame of t1
when you press the button 

BTW, I get

> tk1()
[1] "**** inner objects ****"
[1] "inner"
[1] "**** outer objects ****"
[1] "outer" "tk2"   "tt"   
> [1] "**** inner objects ****"
[1] "inner"
[1] "**** outer objects ****"
[1] "tk1"

consistent with parent.frame() being .GlobalEnv when the button is
pushed. How did you get character(0)??

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list