Re: [R] tklistbox and extracting selection to R

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Tue, 22 Jul 2008 11:17:00 -0600

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.
> ______________________________________________ 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.
Received on Tue 22 Jul 2008 - 17:23:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jul 2008 - 17:31:57 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive