6

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()
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360

3 Answers3

4

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
  • 118,240
  • 47
  • 247
  • 360
2

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

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 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 Apr 22 '10 at 08:54
  • Okay, I'm admitting defeat on this. – Richie Cotton Apr 30 '10 at 13:46
  • 1
    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 Apr 30 '10 at 13:58
1

While the question title is ambiguous whether the talk about forcing visual refresh or just changing the content, I've recently had similar issue with gstatusbar update before and after long operation. While there is an alternative to REPL named REventLoop, I've found the use of tcl timer quite handy.

tcl("after", 300, my_long_operation)

So I update gstatusbar before long operation, then set up timer that in less than a second will fire my function that takes a while, and at the end of that function I update gstatusbar using something like

svalue(sb) <- "Ready"
mlt
  • 1,595
  • 2
  • 21
  • 52