Saving selected values in gcheckboxgroup in R?

103 Views Asked by At

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

There are 2 best solutions below

0
Canada2015 On BEST ANSWER

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 = " ")


})
0
jverzani On

Try this, it is less hassle:

library(gWidgets2)

w <- gwindow("checkbox example")

gp <- ggroup(container=w)

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

g <- ggroup(cont=w, horizontal=FALSE)
cbg <- gcheckboxgroup(codes, cont=g)

selected_codes <- paste(svalue(cbg))

ff <- function(h,...) {

selected_codes <<- svalue(cbg)

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

(You may have had issues due to the containers.)