How to trigger the handler of a button? - r

How is it possible to trigger the handler of a button in gWidgets2? Take this example:
w <- gwindow("Buttons", visible=FALSE)
g <- ggroup(cont=w, horizontal=FALSE)
## various buttons
## without icon
b2 <- gbutton("ouvrir", cont=g)
## with a handler
b4 <- gbutton("click me", cont=g, handler=function(h,...) {
if(svalue(b2) == "open")
svalue(b2) <- "ouvrir"
else
svalue(b2) <- "open"
})
visible(w) <- TRUE
I can trigger the handler of b4 by using the mouse and "physically" clicking on the button. But how can I achieve this via some R code? I am hoping for something like activateHandler(b4) or similar.

As per the suggestions in the comments, I can either via b4$invoke_change_handler(). Or by redefining the handler as a separate function:
w <- gwindow("Buttons", visible=FALSE)
g <- ggroup(cont=w, horizontal=FALSE)
## various buttons
## without icon
h2 <- function(h,...) {
if(svalue(b2) == "open")
svalue(b2) <- "ouvrir"
else
svalue(b2) <- "open"
}
b2 <- gbutton("ouvrir", cont=g)
## with a handler
b4 <- gbutton("click me", cont=g, handler=h2)
visible(w) <- TRUE
and then calling that: h2().

Related

Toggle a gbutton in gWidgets2

I am creating a gbutton in gWidgets2 which will toggle between "go" and "stop" when it will be clicked.
I am following this example: toggling a group of icons in gWidgets
My Code:
library(gWidgets2)
library(gWidgets2RGtk2)
options(guiToolkit="RGtk2")
w= gwindow()
g1 <- ggroup(horizontal=TRUE, cont=w)
icon <- gbutton('go', container = g1)
state <- FALSE # a global
changeState <- function(h,...) {
if(state) {
svalue(icon) <- "go"
} else {
svalue(icon) <- "stop"
}
state <<- !state
}
addHandlerClicked(icon, handler=changeState)
It creates a button and toggle between "go" and "stop" when it is clicked. But the problem is I have to click two times to toggle. I want it should toggle between "go" and "stop" on one click.
You can use blockHandlers() and unblockHandlers() functions to avoid this issue.
w= gwindow()
g1 <- ggroup(horizontal=FALSE, cont=w)
icon <- gbutton("go", container = g1)
#icon <- gimage(reject,cont=g1)
state <- FALSE # a global
addHandlerClicked(icon, function(h,...) {
#
if(!state) {
blockHandlers(icon)
svalue(icon) <- "stop"
unblockHandlers(icon)
} else {
blockHandlers(icon)
svalue(icon) <- "go"
unblockHandlers(icon)
}
state <<- !state
})
I tried this and it works for me.

Display message and rerun the code using gWidgets in R

x <- gconfirm("Run Program?",title="gConfirm")
if (x){
w <- gwindow(title="List of Programs",visible=TRUE)
g = ggroup(horizontal = FALSE, cont=w)
glabel("Please select the Program", cont=g)
ptype <- c("A","B")
temp <- gcombobox(ptype , cont=g)
addHandlerChanged(temp , handler=function(...){})
gbutton("Run", cont=g,handler = function(...){
print(svalue(temp)
dispose(g)
runagain <- gconfirm("Run again?",title="gConfirm")
if(runagain){
## If user clicks okay, I want to start running again from the third line of the code, w <- gwindow......)**
}
}
Can anyone hint a work around that? Also how do I display the svalue(temp) in to similar UI box instead of printing it on the console. Any help is much appreciated.
SOmething like this might get you what you want:
library(gWidgets2)
programs = list("Program A"="a.R",
"Program B" = "b.R")
w <- gwindow("run programs")
g <- ggroup(cont=w, horizontal=FALSE)
fl <- gformlayout(cont=g)
cb <- gcombobox(names(programs), cont=fl, label="Select a program to run")
b <- gbutton("Run selected program", cont=fl, label="", handler=function(h,...) {
prog <- svalue(cb)
val <- gconfirm(sprintf("Run program %s?", prog), parent=w)
if (val) {
source(programs[[val]])
gmessage("All done", parent=w)
}
})

toggling a group of icons in gWidgets

Adapting an example I can toggle the display of an icon like this:
reject <- "D:/Pictures/web/close32.png"
accept <- "D:/Pictures/web/open32.png"
w= gwindow()
g1 <- ggroup(horizontal=TRUE, cont=w)
icon <- gimage(reject,cont=g1)
state <- FALSE # a global
changeState <- function(h,...) {
if(state) {
svalue(icon) <- reject
} else {
svalue(icon) <- accept
}
state <<- !state
}
addHandlerClicked(icon, handler=changeState)
However, I would like to get this to work with a group of icons
example 3x3 icon grouping http://cran.r-project.org/web/packages/gWidgets/vignettes/gWidgets.pdf
so that each icon can be toggled and I can retrieve the state of the icons as a vector. The purpose is to create a graphical selector for picking pairs of observations to perform analysis on. Here is my attempt. It displays correctly, but does not respond to clicks to change the state. I recognize that I am confusing how the handler and action parameters act together and would appreciate any clarifications and fixes for this code.
reject <- "D:/Pictures/web/close32.png"
accept <- "D:/Pictures/web/open32.png"
w= gwindow()
g1 <- ggroup(horizontal=TRUE, cont=w)
lyt <- glayout(cont=g1, spacing=10)
icon <- rep(reject,times=9)
state <- rep(FALSE, times=9)
changeState <- function(h,...) {
if(state[index]) {
svalue(icon[index]) <- reject
} else {
svalue(icon[index]) <- accept
}
state[index] <<- !state[index]
}
for(i in 1:3){
for(j in 1:3){
ind <- (i-1) * 3 +j
lyt[i,j] <- gimage(icon[ind], cont=lyt)
addHandlerClicked(lyt[i,j], handler=changeState, action= index <-ind)
}
}
1c: http://i.stack.imgur.com/4kbwK.png
The index value must be retrieved from h$action in your handler (index <- h$action). As well, this bit action=index <- ind need only be action=ind.

How to trigger the handler of a button using an `alt+letter` keybinding?

In various programs you can often activate a button using an alt+c key binding, if for example the button in question is Close. How is it possible to do this in gWidgets2? Take this example:
w <- gwindow("Buttons")
g <- ggroup(cont=w, horizontal=FALSE)
## various buttons
## without icon
b2 <- gbutton("ouvrir", cont=g)
## with a handler
b4 <- gbutton("click me", cont=g, handler=function(h,...) {
if(svalue(b2) == "open")
svalue(b2) <- "ouvrir"
else
svalue(b2) <- "open"
})
How can I have the c underlined in click me, and the handler of the b4 button activated when the user activates alt+c?

How to disable OK button in `gbasicdialog()` instance of gWidgets2?

Consider:
fix_df <- function(DF, ...) {
dfname <- deparse(substitute(DF))
w <- gbasicdialog(..., handler=function(h,...) {
assign(dfname, df[,], .GlobalEnv)
})
g <- ggroup(cont=w, horizontal=FALSE)
glabel("Edit a data frame", cont=g)
df <- gdf(DF, cont=g, expand=TRUE)
size(w) <- c(400, 400)
out <- visible(w)
}
m <- mtcars[1:3, 1:4]
fix_df(m)
How can I programmatically disable the OK button in w?
There is a do.buttons argument to not show the buttons. To access the button requires digging into the internals. You can see how that is hacked in where do.buttons is implemented in gWidgets2RGtk2.

Resources