Q:

R gWidgets drag and drop between two gtables

I am writing a GUI with the gwidgets package in R. I am implementing a tool that will allow the user to select a number of variables from one list, and drag them to an empty list for collection. The inspiration is the gui from SPSS, see link:
spss drag and drop
I was planning do this with two gtables, i.e. first create one gtable with the list of variables, and an empty gtable to collect the selected variables. Below is my sample code:
  portfolioBuilder <- function(h,...){
  ## globals
  widgets <- list()
  varNames <- c("var1","var2","var3","var4" )#with(.GlobalEnv, names(data))

  #window
  win <- gwindow("Test")

  #groups
  g <- ggroup(horizontal = FALSE, container = win, expand = TRUE)
  gg <- ggroup(horizontal = FALSE, container = win, expand = TRUE)

  #graphics container
  ggraphics(container = gg)

  #paned group
  pg <- gpanedgroup(container = g, expand = TRUE)
  nb <- gnotebook(container = pg)

  ## main group
  qpg <- ggroup(horizontal = FALSE, container = nb, label = "portfolio")
  parg <- ggroup(horizontal = FALSE, container = nb, label = "portfolio args")

  ## qplot group
  tbl <- glayout(container = qpg)

  #variable list
  tbl[1,1,anchor = c(1,0)] <- "Variables"
  tbl[2:10,2] <- (widgets[["table"]] <- gtable(varNames, multiple = TRUE, container = tbl, expand = TRUE))
  tbl[3,3, anchor = c(1,0)] <- "y"
  tbl[3,4] <- (widgets[["y"]] <- gedit("", container = tbl))
  tbl[4,3, anchor = c(1,0)] <- "x"
  tbl[4,4] <- (widgets[["x"]] <- gtable(c(""),container = tbl))

  ## make table visible and set tab
  visible(tbl) <- TRUE
  svalue(nb) <- 1

  ##################################end layout#################################

  }

However my sample code spits out an error because of the empty gtable widget. Does anyone know how to accomplish this with gwidgets?

A:

You'll want to work on the layout, but the key to this is addDropSource and addDropTarget:
options(guiToolkit="RGtk2")
library(gWidgets)
w <- gwindow(visible=FALSE)
g <- gpanedgroup(cont=w)

tbl <- gtable(names(mtcars), cont=g)

fl <- gframe("variables", horizontal=FALSE, cont=g)

dep <- gedit(initial.msg="Dependent variable", label="Dependent", cont=fl)
ind <- gedit(initial.msg="Independent variable(s)", label="Independent", cont=fl)

addDropSource(tbl, handler=function(h,...) svalue(h$obj))

addDropTarget(dep, handler=function(h,...) svalue(h$obj) <- h$dropdata)
addDropTarget(ind, handler=function(h,...) {
  cur <- svalue(h$obj)

  new <- ifelse(nchar(cur) > 0, paste(cur, h$dropdata, sep=", "), h$dropdata)
  svalue(h$obj) <- new
})

visible(w) <- TRUE

Drag and drop support in gWidgets is really variable. Of the 6 possibilities for this to work in: gWidgetsRGtk2, gWidgets2RGtk2, gWidgetstcltk, gWidgets2tcltk, gWidgetsQt and gWidgets2Qt this code only worked in gWidgetsRGtk2. 

