How to return value from function in gwidgets

I have the following code to select directories for input and output folders.

require(gWidgets2RGtk2)

input=NULL
win = gwindow("Stage 1")
g = ggroup(horizontal=FALSE,cont=win)
  g1=ggroup(cont=g)
    svalue(input)<-gbutton("Input folder",cont=g1,expand=TRUE,handler=function(...) {
      input=gfile(type="selectdir")
      gmessage(paste0("Input directory set to ",input))
      input
    })
g2=ggroup(cont=g)
    gbutton("Ouput folder",cont=g2,expand=TRUE,handler=function(...) {
      output=gfile(type="selectdir")
      gmessage(paste0("Output directory set to ",output))
    })

However, when run, the code does not return the directories of the input and output folders. Can someone show me what I'm doing wrong here?

+4
source share
1 answer

To describe my comment in detail, an example of using the environment and <<-

require(gWidgets2)
options(guiToolkit="RGtk2")

input <- NULL                           # global
e <- new.env()                          # environment

win = gwindow("Stage 1")
g = ggroup(horizontal=FALSE,cont=win)

gbutton("Input folder",cont=g,handler=function(...) {
  value <- gfile(type="selectdir")
  gmessage(paste0("Input directory set to ",value))
  ## assign via <<-
  input <<- value
  ## assign to an environment
  e$input <- value
})

gbutton("What is stored?", cont=g, handler=function(...) {
  print(sprintf("The global variable has %s", input))
  print(sprintf("The environment stores %s", e$input))
})
+5
source

All Articles