gWidgets为多列返回多个响应

时间:2014-12-01 19:29:38

标签: arrays gwidgets

我之前在此发布过,并且无法通过单一发布的回复找到解决方案。我尝试过另一种让我想要的东西,主要是。如果它在一个对话框中,当前的解决方案会更好,但我不知道如何做到这一点。我正在整理我在Nabble和堆栈上找到的其他解决方案,但是无法得到我需要的东西。我希望以下内容在单个窗口中提供选项,而不是将用户输入到3个单独的框中。请帮忙。

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 个答案:

答案 0 :(得分:0)

你使它变得比需要的要复杂得多。

  • 最好使异步(在大多数情况下)使用在选择值时调用处理程序。

  • 但是,如果您希望以这种方式完成,请为您的选择小部件创建一个容器,然后将它们添加到容器中。然后将此容器传递到gbasicdialog中。这样一切都将在一个窗口中显示。使用gWidgets2可以更轻松地使用基本对话框。

答案 1 :(得分:0)

好的,这是一个例子,虽然我不太确定你要解决什么问题,所以可能会关闭:

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)

拿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)
}