how to make reactive combobox in r using gWidgets2

2019-09-02 09:43发布

问题:

For my GUI, i want to have 2 combo box.

combobox 1 to display Departments

combobox 2 to display items in the selected department from combobox1

So if the user selects "Electronics" as department in first combobox, productElectronics should be selected for 2nd comboBox else productArts should be selected.

library(gWidgets2RGtk2)
deptnames <- c("Arts","Electronics")
productArts <- c("Beads","Crayons")
productElectronics <- c("iPad","Apple Watch")

a1 <-c()
w <- gwindow("combobox example")
gp <- ggroup(horizontal = FALSE,container=w)
dept <- gcombobox(deptnames, container = gp )

items <- gcombobox(a1, container = gp ,
               handler = function(h,...){
                # oldval <- svalue(dept)
                 if (svalue(dept) == "Arts")
                 {
                   a1 <- productArts
                 }
                 if(svalue(dept) == "Electronics")
                 {
                   a1 <- productElectronics                      
                 }
               }
)

For the above code nothing populates for any value selected in department combobox

回答1:

You should put a handler on the deptnames combobox to update the items combobox. You can mutate the items to select from with items[] <- ... and specify the selected one with svalue(items, index=true) <- .... These would be based on the currently selected value of deptnames which is available through svalue(deptnames). Hope that helps...