[R] tklistbox and extracting selection to R

Greg Snow Greg.Snow at imail.org
Tue Jul 22 19:17:00 CEST 2008


The <<- assignment can be very tricky and Tk windows have their own world.  If you just copy and paste the below (with ntx and levels.tx defined, it would have been nice to have the example fully self running), then the OnOk function has been defined and linked to the button, but never run, so when it looks for tx.choice1, it has not been defined yet.  But after you make a selection and click on ok, then it has.  I prefer to do this type of thing fully in a function (keep all the varibles defined in a local environment), then return the result.  Something like:

getlegend <- function(treatments){
  if(!require(tcltk)) stop('need tcltk')

  ntx <- length(treatments)
  tx.choice1 <- ''

  tt<-tktoplevel()
  tl<-tklistbox(tt,height=ntx,selectmode="single",background="white")
  tkgrid(tklabel(tt,text="Select the legend of" ))
  tkgrid(tl)

  for(i in (1:ntx))
  {
      tkinsert(tl,"end",treatments[i])
  }

  OnOK<-function()
  {
      tx.choice1<<-treatments[as.integer(tkcurselection(tl))+1]
      tkdestroy(tt)

  }
  OK.but<-tkbutton(tt,text="    OK    ", command=OnOK)
  tkgrid(OK.but)

  tkwait.window(tt)
  return(tx.choice1)
}

Then you can run the getlegend function like:

> getlegend( levels(state.division) )

And it will wait until you select something and click on OK, then return the selection (which the user can save or do whatever they want with).

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111



> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Judith Flores
> Sent: Tuesday, July 22, 2008 10:41 AM
> To: RHelp
> Subject: [R] tklistbox and extracting selection to R
>
> Dear experts,
>
>    I am trying to understand why is it that when I paste
> (into the R console) the following code to select an option
> from a list:
>
>   require(tcltk)
>   tt<-tktoplevel()
>   tl<-tklistbox(tt,height=ntx,selectmode="single",background="white")
>   tkgrid(tklabel(tt,text="Select the legend of" ))
>   tkgrid(tl)
>   treatments<<-levels.tx
>   for(i in (1:ntx))
>   {
>       tkinsert(tl,"end",treatments[i])
>   }
>
>   OnOK<-function()
>   {
>       tx.choice1<<-treatments[as.integer(tkcurselection(tl))+1]
>       tkdestroy(tt)
>
>   }
>   OK.but<-tkbutton(tt,text="    OK    ", command=OnOK)
>   tkgrid(OK.but)
>
> tx.choice1
>
>
>      the console can't find tx.choice1, but if I type
> tx.choice1 directly into the console, it returns the value I
> selected from the list.
>
>    I am running R 2.7.1, OS: linnux, I run R within emacs.
>
> Thank you very much for you help,
>
> Judith
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list