[R-gui] How to assign value from tlctk window to variable ?

Ingo Fetzer ingo.fetzer at ufz.de
Mon May 22 13:55:47 CEST 2006


most probably this is a question with a simple answer but I have not found an answer to it anywhere yet:

I took one of James Wettenhalls sample scripts (http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/) for radio buttons as basis to be integrated into a larger script.

Now I wonder how can one assign the input of a tcltk window (eg in the radiobutton
example "oranges") to a variable which can be used later in a r-script ?

 In the case of the radio buttons I tried it with (see also script below):

 chosen.fruit<-tclvalue(rbValue)
 print(chosen.fruit)             #to check the ouput in the console

 But then I always get the default values ("Oranges").
 When I after running the skript print in the R-console "tclvalue(rbValue)" I get the correct values (e.g. "Apples").

 I additionally made rbVal (which should represent the actual input of
 the tcltk window) with "<<-" a global variable but still I get the same
 results as before.

 It would be grateful if one could help me

Ingo



 require(tcltk)
 tt <- tktoplevel()
 rb1 <- tkradiobutton(tt)
 rb2 <- tkradiobutton(tt)
 rbValue <- tclVar("oranges")
 tkconfigure(rb1,variable=rbValue,value="apples")
 tkconfigure(rb2,variable=rbValue,value="oranges")
 tkgrid(tklabel(tt,text="Which do you prefer?"))
 tkgrid(tklabel(tt,text="Apples "),rb1)
 tkgrid(tklabel(tt,text="Oranges "),rb2)

 OnOK <- function()
 {
     rbVal <- as.character(tclvalue(rbValue))
     tkdestroy(tt)
     if (rbVal=="apples")
     	tkmessageBox(message="Good choice!  An apple a day keeps the doctor
 away!")
     if (rbVal=="oranges")
     	tkmessageBox(message="Good choice!  Oranges are full of Vitamin C!")
 }
 OK.but <- tkbutton(tt,text="OK",command=OnOK)
 tkgrid(OK.but)
 tkfocus(tt)

 chosen.fruit<-tclvalue(rbValue)
 print(chosen.fruit)



More information about the R-SIG-GUI mailing list