rgwidgets

Saving selected values in gcheckboxgroup in R?


The following R script is a simple GUI using gWidgets.

I was wondering why this code does not save the selected values by user in gcheckboxgroup.

#### Clear the Global Environment:
rm(list=ls())

library(rattle)
library(RGtk2)
library(gWidgets)
library(tcltk)
library(lubridate)

w <- gwindow("checkbox example")

gp <- ggroup(container=w)

codes = c(
  "1000 F",
  "0100 Q",
  "0010 M",
  "0001 s")

cbg <- gcheckboxgroup(codes, cont=w)

selected_codes <- paste(svalue(cbg))

ff <- function(h,...) 

selected_codes <- svalue(cbg)

obj_run <- gbutton("Run", container=w, handler = ff)

Solution

  • Thanks for jverzani's comment. However, that was not the solution.

    Actually, by clicking the Run button in GUI, we have the selected_code is the R memory. But it can not be saved as it is inside the function/handler. So, we need to save (write) it in a file (.txt for example) using this code:

    rm(list=ls())
    library(rattle)
    library(RGtk2)
    library(gWidgets)
    library(tcltk)
    library(lubridate)
    
    w <- gwindow("checkbox example")
    
    gp <- ggroup(container=w)
    
    codes = c(
      "1000 F",
      "0100 Q",
      "0010 M",
      "0001 s")
    
    cbg <- gcheckboxgroup(codes, cont=w)
    
    
    
    obj_run <- gbutton("Run", container=w, handler = function (h ,...){
      selected_codes <- paste0(svalue(cbg))
      write(selected_codes, file = "selected_codes.txt",
            ncolumns = if(is.character(selected_codes)) 1 else 1,
            append = FALSE, sep = " ")
    
    
    })