gWidgets return multiple responses for multiple columns

120 Views Asked by At

I posted before on this and was not able to arrive at a solution with the single posted response. I have tried another approach that gets me what I want, mostly. The current solution would work better if it were in a single dialogue box but I am not sure how to do this. I am putting together other solutions I found on Nabble and stack and just cannot get what I need. I want the following to offer the choices in a single window, instead of having the user input into 3 separate boxes. Please help.

options(guiToolkit="RGtk2")
library(gWidgets)
#Creat selection function
select <- function(x,multiple = TRUE, Title,...){
  ans<-new.env()

  x1<-ggroup(horizontal=TRUE) # no parent container here
  x2<-gcheckboxgroup( x,multiple=multiple,con=x1,expand=TRUE)
  ret <- gbasicdialog(title = Title, widget=x1,handler=function(h,...){
    value <- svalue(x2)
    if (length(value)==0) value=""
    assign("selected",value,env=h$action$env)
    dispose(x1)
  },action=list(env=ans))
  ans
}   
#Create list to store results
Days. <- c("Day1","Day2","Day3")
Outputs_ <- list()
SelectionOptions. <- c("Bicycle Event1", "Construction Nearby","Path Closure")

#Cycle through each day 
for(day in Days.){      
    ans <- select(SelectionOptions., Title = day)
    Outputs_[[day]] <- ans$selected
}
#return results of selection
unlist(Outputs_)
2

There are 2 best solutions below

1
On

You are making it a bit more complicated than need be.

  • you are better off making things asynchronous (in most cases), using a handler to be called when the values are selected.

  • However, if you want it done this way, make a container for your selection widgets, then add them to the container. This container is then passed into gbasicdialog. That way all will show in one window. Using gWidgets2 makes working with the basic dialog a lot easier.

8
On

Okay, here is an example, though I'm not quite sure what problem you are trying to solve, so may be off:

library(gWidgets2)

Days. <- c("Day1","Day2","Day3")
SelectionOptions. <- c("Bicycle Event1", "Construction Nearby","Path Closure")

w <- gwindow()
g <- gformlayout(cont=w)
days <- gradio(Days., cont=g, label="Days:")
opts <- gcombobox(SelectionOptions., cont=g, label="Options:")

btn <- gbutton("Show them", cont=g, label="")

addHandlerClicked(btn, handler=function(h,...) {
    values <- lapply(list(days, opts), svalue)
    print(values)
})

## or using a modal dialog

dlg <- gbasicdialog(handler=function(h,...) {
    values <- lapply(list(days, opts), svalue)
    print(values)
})
g <- gformlayout(cont=dlg)
days <- gradio(Days., cont=g, label="Days:")
opts <- gcombobox(SelectionOptions., cont=g, label="Options:")
visible(dlg, TRUE)

## third time is a charm? (Though you could clean this up)
library(gWidgets2)
e <- new.env()
e$Days <- ""; e$Sel <- ""

Days. <- c("Day1","Day2","Day3")
SelectionOptions. <- c("Bicycle Event1", "Construction Nearby","Path Closure")

dlg <- gbasicdialog(title="test", do.buttons=FALSE)
g <- ggroup(cont=dlg, horizontal=FALSE)

fl <- gformlayout(cont=g)
days <- gradio(Days., cont=fl, label="Days:")
cb <- gcombobox(SelectionOptions., selected=0, cont=fl, label="Options:")

handler <- function(h,...) {
    e$Days <- svalue(days)
    e$Sel <- ifelse(length(svalue(cb)) > 0, svalue(cb), "")
    if (all(sapply(e, function(x) nchar(x) > 0)))
        dlg$dispose_window()
}
sapply(list(days, cb), addHandlerChanged, handler=handler)
visible(dlg, TRUE)

Take 4

library(gWidgets2)
days <- paste("Day", 1:3)
events <- c("none", "Bicycle Event","Construction Nearby","Path Closure")
l <- list()

handler <- function(h,...) {
    l[[h$action]] <<- svalue(h$obj)
    if(length(l) == 3)
        print(l)
}

w <- gwindow()
g <- gvbox(cont=w)
for (day in days) {
    l <- glabel(day, cont=g); font(l) <- list(weight="bold")
    gradio(events, cont=g, handler=handler, action=day)
}