I have created a two-tab GUI with gWidgets. A graph is embedded in each of the two tabs. The problem is that both graphs are sent to tab 2. How can I switch or choose the active device? The vignette of the gwidgets package suggests either addHandlerChanged or ggraphicsnotebook. In using ggraphicsnotebook, some unwanted buttons are generated. So I am wondering how to do it with addHandlerChanged or other methods. A small example is attached. Thanks.
# library
library(gWidgets); library(cairoDevice)
options(guiToolkit = "RGtk2")
# first tab -----------------------------------------------------------
r.main <- gwindow(title = "Correlation", visible = TRUE)
r.nb <- gnotebook(container = r.main)
rSta <- ggroup(container = r.nb, horizontal = TRUE, label = "Static")
rDyn <- ggroup(container = r.nb, horizontal = FALSE, label = "Dynamic")
ggraphics(container = rSta)
obj.ptNum <- gradio(items = c("100", "1,000", "5,000"),
selected = 2, horizontal = FALSE, container = rSta,
handler = function(h, ...) {plot(1:10, main = "Static graph")})
# second tab -----------------------------------------------
obj.plotNum <- gradio(items = c("10", "50", "300", "400"),
selected = 2, horizontal = TRUE, container = rDyn,
handler = function(h, ...) {
plot(30:35, col = 'red', main = "Dynamic graph")})
ggraphics(container = rDyn)
Edit: I found one solution. Note the problem is not about selecting tab, but about selecting the current active embedded device. For future reference, my code is copied below. One small problem that I cannot figure out: how to show the graph when the application is launched without clicking? Thanks all.
library(gWidgets); library(cairoDevice); library(gWidgetsRGtk2)
library(RGtk2)
options(guiToolkit = "RGtk2")
# first tab -----------------------------------------------------------
r.main <- gwindow(title = "Correlation", visible = TRUE)
r.nb <- gnotebook(container = r.main)
rSta <- ggroup(container = r.nb, horizontal = TRUE, label = "Static")
ggraphics(container = rSta)
staPlot <- function(h, ...) {
sel <- ifelse(test = length(dev.list()) >= 2, yes = 2, no = 1)
dev.set(which = sel)
plot(1:as.numeric(svalue(obj.ptNum)), main = "Static graph")}
obj.ptNum <- gradio(items = c("100", "150", "200"),
selected = 2, horizontal = FALSE, container = rSta,
handler = staPlot)
# second tab -----------------------------------------------
rDyn <- ggroup(container = r.nb, horizontal = FALSE, label = "Dynamic")
dynPlot <- function(h, ...) {
sel <- ifelse(test = length(dev.list()) >= 2, yes = 3, no = 1)
dev.set(which = sel)
plot(1:as.numeric(svalue(obj.plotNum)), col = 'red',
main = "Dynamic graph")}
obj.plotNum <- gradio(items = c("10", "50", "300", "400"),
selected = 2, horizontal = TRUE, container = rDyn,
handler = dynPlot)
ggraphics(container = rDyn)
svalue(r.nb) <- 1