jw9@softhome.net
2002-May-29 18:15 UTC
[R] Tcl/tk , question about the environment of the call
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! -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-May-29 18:40 UTC
[R] Tcl/tk , question about the environment of the call
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks, Peter. Actually I use function()tk2() instead tk2(). I tried just use tk2, the result is the same as yours. By the way, could you provide more detail about how to assign tk2 to a global variable. Thanks! ############################# 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=function()tk2())) tk2() } ############################# -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._