我正在R中创建一个相当简单的GUI,该GUI根据用户选择的值来创建不同数据分析的图形。我在弄清楚如何使用户能够从列表中选择多个值时遇到麻烦。我正在研究的方法如下。问题区域在if语句中,这是我需要将用户选择放入列表的位置。
CallSpecies<-function(options){
dialog<-gtkMessageDialog(NULL,0,"question","ok-cancel","Choose a species",show=FALSE)
sppmodel<-rGtkDataFrame(Species)
sppview<-gtkTreeView(sppmodel)
sppview$getSelection()$setMode("multiple")
column<-gtkTreeViewColumn("Species Code",gtkCellRendererText(),text=0)
column1<-gtkTreeViewColumn("Common Name",gtkCellRendererText(),text=1)
sppview$appendColumn(column)
sppview$appendColumn(column1)
scrolled_window<-gtkScrolledWindow()
scrolled_window$setSizeRequest(-1,150)
scrolled_window$add(sppview)
dialog[["vbox"]]$add(scrolled_window)
if (dialog$run()==GtkResponseType["ok"]){
}
dialog$destroy()
}
答案 0 :(得分:0)
大量阅读其他语言的文档并将其翻译为R语法后,我终于解决了自己的问题。我已经编辑了代码以显示我的解决方案。我绝不是R或GTK的专家,但这似乎行得通。
CallSpecies<-function(options){
dialog<-gtkMessageDialog(NULL,0,"question","ok-cancel","Choose a species. Hold control and click to select multiple species.",show=FALSE)
sppmodel<-rGtkDataFrame(Species)
sppview<-gtkTreeView(sppmodel)
sppview$getSelection()$setMode("multiple")
column<-gtkTreeViewColumn("Species Code",gtkCellRendererText(),text=0)
column1<-gtkTreeViewColumn("Common Name",gtkCellRendererText(),text=1)
sppview$appendColumn(column)
sppview$appendColumn(column1)
scrolled_window<-gtkScrolledWindow()
scrolled_window$setSizeRequest(-1,150)
scrolled_window$add(sppview)
dialog[["vbox"]]$add(scrolled_window)
spplist<-c()
if (dialog$run()==GtkResponseType["ok"]){
selection<-sppview$getSelection()
sel_paths<-selection$getSelectedRows()$retval
i=1
for (p in sel_paths){
sel_row<-sel_paths[[i]]$getIndices()[[1]]
sel_row<-sel_row+1
elem<-Species[sel_row,"SpeciesCode"]
elem<-as.character(elem)
spplist<-c(spplist,elem)
i<-i+1
}
print("spplist")
print(spplist)
}
dialog$destroy()
}