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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon May 22 14:22:01 CEST 2006


Ingo Fetzer <ingo.fetzer at ufz.de> writes:

> 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").

Er, what did you expect? chosen.fruit will represent the value for the
selected item  *at the time of the assignment* .
 
>  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)
> 
> _______________________________________________
> R-SIG-GUI mailing list
> R-SIG-GUI at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-gui
> 

-- 
   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
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-SIG-GUI mailing list