Peter Wolf
2000-Sep-29 08:14 UTC
Two tcltk questions and Re: [R] tcltk package functionality
Sorry, for my mail from last night contains no subject. Therefore, I send it again and two tcltk questions are appended. ---------------------------------------------------------------------------- Prasad wrote:> I wrote a function in R which uses tcltk package .... essentially I wanted > to give within that function, a widget with 2 radiobuttons to choose > between plotting Precip and Temperature plots. After the user has chosen > one of the radiobuttons there is another widget that asking him to identify > outliers. However, I am having a lot of problems...what R does is evaluate > the whole function without pausing...if I introduce a while() loop as I > have in the example below, R does nothing until I hit cntrl-c upon which it > shows the widget and comes out of the function......I could use a while > loop in S-PLUS using the dialog.create() dialog.display() functions, but I > cannot seem to implement that functionality in R.....what am I doing wrong? > I enclose below the sample function...Any help will be greatly > appreciated.... >To stop the evaluation of a function until a specific tcltk action is done you have to use the tk-function tkwait.variable(). The following function -- a simple modification of Prasad's tcltktst function -- shows an example: tcltk.test <- function(x1=1:10, x2=10:1) { library("tcltk") # define first toplevel-widget tt <- tktoplevel() tktitle(tt) <- "Diagnostics" label.widget <- tklabel(tt, text="Choose data for plot!") rbut.wid1 <- tkradiobutton(tt, text="x1", value="0", variable="choice") rbut.wid2 <- tkradiobutton(tt, text="x2", value="1", variable="choice") but.done <- tkbutton(tt, text="FINISHED", command=function(){ tclvar$done <- "T" tkdestroy(tt) } ) tkpack(label.widget, rbut.wid1, rbut.wid2, but.done) # wait until FINISHED is pressed tclvar$choice <- "0" tkwait.variable("done") # plot x1 or x2 if(tclvar$choice == "0") x <- x1 if(tclvar$choice == "1") x <- x2 if(is.null(names(x))) names(x) <- x plot(x) # define second toplevel widget tt2 <- tktoplevel() tktitle(tt2) <- "Action" but.wid21 <- tkbutton(tt2, text="print summary", command=function()print(summary(x))) but.wid22 <- tkbutton(tt2, text="identify outlier", command=function()identify(x)) but.wid23 <- tkbutton(tt2, text="exit", command=function(){ tclvar$done<-"T" tkdestroy(tt2) } ) tkpack(but.wid21, but.wid22, but.wid23) # wait until exit is pressed tclvar$done <- "F" tkwait.variable("done") } -------------------------------------------------------------------------- Here are another two tcltk question: 1. I want to add a scrollbar along the right side of a text widget. This can be achieved by .Tk.ID(). Is there a way to avoid to call .Tk.ID --- perhaps by using tkyview? tk.test1 <- function(nmax=3){ library("tcltk") tl <- tktoplevel() tl.text <- tktext(tl) tl.yscroll <- tkscrollbar(tl) tkconfigure(tl.text, yscrollcommand=paste(.Tk.ID(tl.yscroll),"set" )) tkconfigure(tl.yscroll, command=paste(.Tk.ID(tl.text), "yview")) tkpack(tl.text, tl.yscroll, side="left", fill="y") tkpack(tkbutton(tl, text="exit", command=function()tclvar$done<-"T")) tclvar$done <- "F"; tkwait.variable("done") tkdestroy(tl) } 2. If I try to insert a text containing an unequal number of "{" and "}" brackets tkinsert stops with the error message: Error in .Tcl(.Tcl.args(...)) : [tcl] extra characters after close-brace. How can I change this behavior? tk.test2 <- function(nmax=3){ library("tcltk") tl <- tktoplevel() tl.text <- tktext(tl) tkpack(tl.text) for(i in 1:nmax){ cat("input text? "); input.text<-readline() tkinsert(tl.text,"0.0",input.text) } tkdestroy(tl) } -------------------------------------------------------------------------- Version information: platform hppa2.0-hp-hpux10.20 major 1 minor 1.0 year 2000 month June day 15 -------------------------------------------------------------------------- Peter Wolf R + tktcl is a very fantastic combination! Thanks to all of the Core team! -------------------------------------------------------------------------- Hans Peter Wolf pwolf at wiwi.uni-bielefeld.de Fak. f. Wiwi. Uni Bielefeld 33615 Bielefeld Germany ------------------------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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
2000-Sep-29 16:42 UTC
Two tcltk questions and Re: [R] tcltk package functionality
Peter Wolf <pwolf at wiwi.uni-bielefeld.de> writes:> Here are another two tcltk question: > > 1. I want to add a scrollbar along the right side of a text widget. This can be > achieved by .Tk.ID(). Is there a way to avoid to call .Tk.ID --- > perhaps by using tkyview? > > tk.test1 <- function(nmax=3){ > library("tcltk") > tl <- tktoplevel() > tl.text <- tktext(tl) > tl.yscroll <- tkscrollbar(tl) > tkconfigure(tl.text, yscrollcommand=paste(.Tk.ID(tl.yscroll),"set" )) > tkconfigure(tl.yscroll, command=paste(.Tk.ID(tl.text), "yview")) > tkpack(tl.text, tl.yscroll, side="left", fill="y") > tkpack(tkbutton(tl, text="exit", command=function()tclvar$done<-"T")) > tclvar$done <- "F"; tkwait.variable("done") > tkdestroy(tl) > }One problem with this stuff is that the users seem to be quickly becoming more proficient than the programmer... The first one is avoidable by a construction like tkconfigure(tl.text, yscrollcommand=function(...)tkset(tl.yscroll,..2,..3)) I.e. you get a callback to R instead. The yscrollcommand option works by sticking arguments at the end of what the .Tcl.callback generates for the function, so the callback is actually called with arguments (%...,x,y) and you need to extract (x,y) by the above kludge (or something equally kludgy). Ick. Similarly, one can do tkconfigure(tl.yscroll, command=function(...)tkyview(tl.yscroll,..2,..3)) *but* there's a varying number of arguments in this case, so it only works on the slider of the scrollbar, not the arrowheads at the end. You need to apply an alternate kludge to pass all arguments except the first on to tkyview (exercise for the reader...) Alternatively tell me how to avoid producing that "%..." argument on callbacks and command=function(...)tkyview(tl.yscroll,...) should work. I suspect that it might be easy.> 2. If I try to insert a text containing an unequal number of "{" and "}" > brackets tkinsert stops with the error message: > > Error in .Tcl(.Tcl.args(...)) : [tcl] extra characters after close-brace. > > How can I change this behavior? > > tk.test2 <- function(nmax=3){ > library("tcltk") > tl <- tktoplevel() > tl.text <- tktext(tl) > tkpack(tl.text) > for(i in 1:nmax){ > cat("input text? "); input.text<-readline() > tkinsert(tl.text,"0.0",input.text) > } > tkdestroy(tl) > }By fixing the bug ;) This has been waiting to happen. The quoting mechanisms of Tcl makes it impossible to get an unbalanced set of braces into a {...}-delimited string and this is what the .Tcl interface is producing. So one really needs to use "..." delimiting with the entire morass of special character escapes. This is due to sheer laziness on my part (or slightly nicer put: my mind was focused elsewhere at the time). -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._