Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return values from gWidgets and handlers?

Tags:

r

gwidgets

I am trying to develop a GUI (using gWidgets) for an R package. My plan was to construct a main window holding the data, and with buttons calling small gui wrappers for each function. Unfortunately I am stuck on a basic(?) problem - I don't know how to transfer the data.

Questons:

  • How to properly send data between separate windows?
  • How to send data from within a handler in another window?

My problem is similar to: Loading and saving variables in R with gWidgets, but from what I have read the use of .GlobalEnv is not recommended.

I have also seen someone using the <<- operator: http://www.mail-archive.com/[email protected]/msg00053.html, but I can't reproduce it properly (and it will not work with my example, I think).

Below is a simple example, where I try to send a text to another window and back again if the button is pressed. I have tried with return inside the handler, but that doesn't work (also not sure if it is allowed). The subwindow immediately return its value at the end of the function, before the handler/inner function can act on the data. I don't know how to reach out from the handler to the main window.

main <- function(){

  library(gWidgets)
  options(guiToolkit="RGtk2")

  w <- gwindow(title="Main window",
               visible=FALSE)

  txt <- gtext(text="Initial text in main window.",
               container=w)

  btn <- gbutton("Send to sub window", container=w)

  addHandlerChanged(btn, handler = function(h, ...) {
    shouldbenew <- subwindow(svalue(txt))
    svalue(txt) <- paste("The sub window immediately returns 'TRUE', before pushing 'Return to main':", shouldbenew )
  } )

  visible(w) <- TRUE

}

subwindow<- function(text){

  library(gWidgets)
  options(guiToolkit="RGtk2")

  sw <- gwindow(title="Sub window",
                visible=FALSE)

  editedtxt <- gtext(text=paste(text, "- Text is transferred to the sub window, but I don't know how to send it back to the main window"),
                     container=sw)

  btn <- gbutton("Send to main window", container=sw)

  addHandlerChanged(btn, handler = function(h, ...) {
    newtxt <- svalue(editedtxt)
    return(newtxt)

  } )

  visible(sw) <- TRUE

}

Update: Here is the solution I picked as the way forward (as suggested by jverzani), illustrated using the example above. I hope I understood the suggested solution correct and that I have implemented it in a 'nice' way, ideally accepted at CRAN.

To summarise I created a new environment within the main window environment. I edited the sub window to take the environment in the call. Pressing the button in the sub window assign the edited text to the passed environment. When the sub window is closed, and the main window comes into focus, the edited text is accessible from the environment using get.

main <- function(){

  library(gWidgets)
  options(guiToolkit="RGtk2")
  # Create a new environment for passing data.
  .mainGlobal <- new.env()

  w <- gwindow(title="Main window", visible=FALSE)

  txt <- gtext(text="Initial text in main window.",
               container=w)

  btn <- gbutton("Send to sub window", container=w)

  addHandlerChanged(btn, handler = function(h, ...) {
    # Call sub widget passing text and environment.
    subwindow(text=svalue(txt), env=.mainGlobal)
  } )

  visible(w) <- TRUE

  addHandlerFocus(w, handler = function (h, ...) {

    if(exists("myText", envir=.mainGlobal)){
      # Retrieve text and update.
      svalue(txt) <- get("myText", envir=.mainGlobal)
    }    
  })

}

subwindow<- function(text, env=NULL){

  library(gWidgets)
  options(guiToolkit="RGtk2")

  sw <- gwindow(title="Sub window", visible=FALSE)

  editedtxt <- gtext(text=text, container=sw)

  btn <- gbutton("Send to main window", container=sw)

  addHandlerChanged(btn, handler = function(h, ...) {
    newtxt <- svalue(editedtxt)
    assign("myText", newtxt, envir=env)
  } )

  visible(sw) <- TRUE

}
like image 461
Oskar Hansson Avatar asked Mar 16 '13 22:03

Oskar Hansson


2 Answers

You can just return the widgets for each window in a list. So the main function adds the line list(w = w, txt = txt, btn = btn) at the end to return each widget and make them accessible after the function has finished.

The following example is the smallest change to your code that works , but there's a minor flaw in that main and subwindow now contain references to the return values from each other. The code works, but if you are doing something more complicated, it could easily become hard to maintain.

library(gWidgets)
options(guiToolkit="tcltk")

main <- function(){
  w <- gwindow(title="Main window",
               visible=FALSE)

  txt <- gtext(text="Initial text in main window.",
               container=w)

  btn <- gbutton("Send to sub window", container=w)

  addHandlerChanged(btn, handler = function(h, ...) {
    svalue(subWindow$txt) <- svalue(mainWindow$txt)
  } )

  visible(w) <- TRUE
  list(w = w, txt = txt, btn = btn)
}

subwindow<- function(text){
  sw <- gwindow(title="Sub window",
                visible=FALSE)

  editedtxt <- gtext(text="",
                     container=sw)

  btn <- gbutton("Send to main window", container=sw)

  addHandlerChanged(btn, handler = function(h, ...) {
    svalue(mainWindow$txt) <- svalue(subWindow$txt)
  } )

  visible(sw) <- TRUE
  list(w = sw, txt = editedtxt, btn = btn)
}

mainWindow <- main()
subWindow <- subwindow()
like image 114
Richie Cotton Avatar answered Nov 17 '22 05:11

Richie Cotton


You can pass information between gwidgets by using independent functions and without beforehand knowing the object name of the receiver widget:

initMain <- function() {
  w <- gwindow(title="Main window",visible=FALSE)
  txt <- gtext(text="Initial text in main window.",container=w)
  btn <- gbutton("Send to sub window", container=w)

  list(
    run = function(partner) {
      addHandlerChanged(btn, handler = function(h, ...) {
        svalue(partner$txt) <- svalue(txt)
      } )
      visible(w) <- TRUE
    },
    txt = txt
  )
}

initSubWindow<- function() {
  w <- gwindow(title="Sub window",visible=FALSE)
  txt <- gtext(text="huhu",container=w)
  btn <- gbutton("Send to main window", container=w)

  list(
    run = function(partner) {
      addHandlerChanged(btn, handler = function(h, ...) {
        svalue(partner$txt) <- svalue(txt)
      } )
      visible(w) <- TRUE
    },
    txt = txt
  )
}

mw <- initMain()
sw <- initSubWindow()

mw$run(sw)
sw$run(mw)
like image 43
Egus Avatar answered Nov 17 '22 03:11

Egus