简体   繁体   中英

Allowing user to select multiple values from a list with RGtk2 in R?

I am creating a fairly simple GUI in R that creates figures of different data analysis based on values selected by a user. I am having trouble figuring out how to enable a user to select multiple values from a list. The method I am working on is below. The problem area is in the if statement which is where I need to place the user selections into a list.

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()

}

I ended up solving my own issue after quite a bit of reading documentation from other languages and translating it to R syntax. I have edited the code to show my solution. I am by no means an expert in R or GTK but this seems to work.

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()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM