How do you refresh the contents of an R gWidget?
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 = 开发者_JS百科grp,
handler = function(h, ...)
{
if(exists("grp") && !is.null(grp))
{
delete(win, grp)
}
create.widgets()
}
)
}
win <- gwindow()
create.widgets()
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.
AFAIK those refresh events are often owned by the window manager so this may be tricky.
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"
精彩评论