tags:

views:

74

answers:

2

I'm creating a GUI in R using gWidgets (more specifically gWidgetstcltk). I'd like to know how to update the contents of selection-type widgets, such as gdroplist and gtable. I currently have a rather hackish method of deleting the widget and re-creating it. I'm sure there's a better way.

This simple example displays all the variables in the global environment.

library(gWidgets)
library(gWidgetstcltk)

create.widgets <- function()
{
  grp <- ggroup(container = win)
  ddl <- gdroplist(ls(envir = globalenv()), 
    container = grp)
  refresh <- gimage("refresh", 
    dirname   = "stock",
    container = grp,
    handler   = function(h, ...)
    {
      if(exists("grp") && !is.null(grp)) 
      {
        delete(win, grp)
      }
      create.widgets()   
    }
  )
}

win <- gwindow()
create.widgets()
+1  A: 

AFAIK those refresh events are often owned by the window manager so this may be tricky.

Dirk Eddelbuettel
In that case, how low do I have to go to get access to this kind of functionality? Will the `tcltk`/`tcltk2` packages do the trick? Is it possible to directly access to the window manager from R?
Richie Cotton
Okay, I'm admitting defeat on this.
Richie Cotton
There is no such thing as defeat -- but for this you may have to forgo _portability_ as it is so dependent on the window manager. One possible ray of hope may be the binding to Qt that Deepayan and Michael have been working on. One day these may migrate from R-Forge to CRAN and give you an alternative.
Dirk Eddelbuettel
A: 

I spoke to John Verzani, creator of the gWidgets* packages, and the answer is incredibly simple (though not entirely intuitive). You access the contents of list-type widgets with widget_name[].

library(gWidgets)
library(gWidgetstcltk)

get_list_content <- function() ls(envir = globalenv())  # or whatever

win <- gwindow()
grp <- ggroup(container = win)
ddl <- gdroplist(get_list_content(), container = grp)
refresh <- gimage("refresh", 
  dirname   = "stock",
  container = grp,
  handler   = function(h, ...) ddl[] <- get_list_content()   
)

Note that there are some restrictions: radio button lists must remain the same length.

win <- gwindow()
rb <- gradio(1:10, cont = win)
rb[] <- 2:11     # OK
rb[] <- 1:5      # Throws an error; can't change length.
Richie Cotton