rgwidgets

gtable (from R package gWidgets2) returns previous selection


The gtable widget from the gWidgets2 package in R returns the previously selected value when clicking on a new value. If using the keyboard arrows to select a value and then Enter to activate the current selection is returned. This was unexpected behaviour for me. If this is how it should work by design, how can I get the current selection in both cases?

EDIT: It seems like it works as expected using gWidgets, so it might be a bug in gWidgets2. Start a new R session and change to gWidgets2 -> gWidgets and gvbox -> ggroup in the example below.

require(gWidgets2)

# Select by clicking (Clicked) or hit Enter (Changed)
# Move using mouse or arrow keys.

# Create the example (adopted from the gtable example)
w <- gwindow("gtable example", visible=FALSE)
g <- gvbox(cont=w)
tbl <- gtable(mtcars, cont=g, expand=TRUE, fill=TRUE)

addHandlerClicked(tbl, handler = function(h, ...) {

  print("Clicked returns the previously selected value.")

  print(svalue(tbl))

} )

addHandlerChanged(tbl, handler = function(h, ...) {

  print("Changed returns the currently selected value.")

  print(svalue(tbl))

} )

visible(w) <- TRUE

R version 3.4.1 (2017-06-30), Platform: x86_64-w64-mingw32/x64 (64-bit), Running under: Windows 7 x64 (build 7601) Service Pack 1 Packages: gWidgets2_1.0-7, gWidgets2RGtk2_1.0-6, RGtk2_2.20.33


Solution

  • Answer by @jverzani on GitHub https://github.com/jverzani/gWidgets2/issues/94#issuecomment-316739581

    Sorry, I don't have a good solution here. I thought I did, but can't figure it out. The issue is the gWidgets2 observer is called before the widget is updated. I thought staging the handlers differently would work, but ...

    The workaround would be to not connect this way, but rather use addHandlerSelectionChanged This gives single click and keyboard response, but doesn't play nicely with double click. Alternatively, double click is supposed to "activate" the cell initiating a callback, so not assigning to the click handler is possible and just assigning to the change handler gives you double click and keyboard selection, but not single click, which just sets the selection but doesn't initiate a callback.

    It seem to work as expected in my application. I use only addHandlerChanged in one place and only addHandlerSelectionChanged in another.