Hi everyone, I'm trying to build a conditional or dynamic dropdown list that depends on a value inserted in a tkentry field. The value in this field will be used to subselect a data fame and so defining the values on the dependent dropdown list. This kind of feature is very often found in web based forms, for example: selecting a given state fills up a second dropdown that lists all the cities in the first option. I'm not sure but probably a button should be used to trigger the procedure. Up to now I've made no progress on doing this since all examples or documentation only focus on static dropdowns. Thanks in advance, Jo?o.
[R-gui] TclTk dynamic dropdown list combo box
5 messages · Greg Snow, "João G.", j verzani +1 more
Create and pack a combo box (possibly with dummy entries), then when you want to dynamically change the options call tkconfigure( comboboxobject, values = vectorwithvalues ) I think that should work, but have not tested it specifically for your case (my tkexamp function in the TeachingDemos package does something like this, but not after an entry has been filled).
Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111 > -----Original Message----- > From: r-sig-gui-bounces at stat.math.ethz.ch [mailto:r-sig-gui- > bounces at stat.math.ethz.ch] On Behalf Of "Jo?o G." > Sent: Thursday, June 24, 2010 3:41 PM > To: r-sig-gui at stat.math.ethz.ch > Subject: [R-gui] TclTk dynamic dropdown list combo box > > Hi everyone, > > I'm trying to build a conditional or dynamic dropdown list that depends > on a value inserted in a tkentry field. The value in this field will be > used to subselect a data fame and so defining the values on the > dependent dropdown list. > This kind of feature is very often found in web based forms, for > example: selecting a given state fills up a second dropdown that lists > all the cities in the first option. I'm not sure but probably a button > should be used to trigger the procedure. > Up to now I've made no progress on doing this since all examples or > documentation only focus on static dropdowns. > > Thanks in advance, > Jo?o. > > _______________________________________________ > R-SIG-GUI mailing list > R-SIG-GUI at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/r-sig-gui
Thank you! That worked out nicely.
I leave here the code used in case it turns out useful for someone else:
#
##
require(tcltk)
tclRequire("BWidget")
tt <- tktoplevel()
tkwm.title(tt,"Test GUI")
tkgrid(tklabel(tt,text="Test label"))
ttframe<-tkframe(tt,relief="groove",borderwidth=2)
x<<-cbind(x=rep(LETTERS[1:3],each=4),y=1:12)
v<-tclVar("A")
z<-x[x[,1]==tclvalue(v),2]
entry.v<-tkentry(ttframe,width="30",textvariable=v)
label.v<-tklabel(ttframe,text=tclvalue(v))
tkconfigure(label.v,textvariable=v)
tkgrid(entry.v)
tkgrid(tklabel(ttframe,text=""))
tkgrid(label.v)
getVal<-function(){
z<-x[x[,1]==tclvalue(v),2]
tkconfigure(comboBox,values=z)
}
comboBox<-tkwidget(ttframe,"ComboBox",editable=FALSE,values=z)
button.getValues<-tkbutton(ttframe,text="Get values",command=getVal)
tkgrid(comboBox,button.getValues)
tkgrid(ttframe)
tkgrid(tklabel(tt,text=""))
tkfocus(tt)
#
# Type B or C in the text entry field and press the button "Get values".
# This will update the values in the dropdown field
Best regards,
Jo?o Gon?alves.
24-06-2010 22:50, Greg Snow:
Create and pack a combo box (possibly with dummy entries), then when you want to dynamically change the options call tkconfigure( comboboxobject, values = vectorwithvalues ) I think that should work, but have not tested it specifically for your case (my tkexamp function in the TeachingDemos package does something like this, but not after an entry has been filled).
Jo?o G. <joaofgo <at> gmail.com> writes:
Thank you! That worked out nicely. I leave here the code used in case it turns out useful for someone else:
If you wanted to do this with gWidgetstcltk you could do this:
library(gWidgets)
options(guiToolkit="tcltk")
d <- mtcars ## some data frame
w <- gwindow()
g <- ggroup(cont=w, horizontal=FALSE)
namesCb <- gcombobox(names(d),selected=1, cont=g)
valsCb <- gcombobox(c(""), cont=g)
updateCb <- function(varname) {
valsCb[] <- d[[varname]]
svalue(valsCb, index=TRUE) <- 1
}
addHandlerChanged(namesCb, function(h,...) updateCb(svalue(h$obj)))
updateCb(svalue(namesCb))
--John
2 days later
Thank you for taking interest in this problem. Both solutions proposed worked out very smoothly. I'm certainly going to use the gWidgets package more often in my future R projects. Best regards, Jo?o Gon?alves
jverzani wrote:
Jo?o G.<joaofgo<at> gmail.com> writes:
Thank you! That worked out nicely.
I leave here the code used in case it turns out useful for someone else:
If you wanted to do this with gWidgetstcltk you could do this:
library(gWidgets)
options(guiToolkit="tcltk")
d<- mtcars ## some data frame
w<- gwindow()
g<- ggroup(cont=w, horizontal=FALSE)
namesCb<- gcombobox(names(d),selected=1, cont=g)
valsCb<- gcombobox(c(""), cont=g)
updateCb<- function(varname) {
valsCb[]<- d[[varname]]
svalue(valsCb, index=TRUE)<- 1
}
addHandlerChanged(namesCb, function(h,...) updateCb(svalue(h$obj)))
updateCb(svalue(namesCb))
--John