I am trying to create a simple GUI in R. In this GUI I want to have two combobox, 1) List of data frame from the current workspace 2) Names of variable corresponding to the data frame selected in first combobox. I am trying to use gWidgets library but I am not sure how to do it. Can anyone in this forum help me? Thanks
I want SourceVar as a combo box by taking variable names from inData argument
## list data frames in an environment
lsDF <- function(envir=.GlobalEnv) {
varNames <- ls(envir=envir)
dfs <- sapply(varNames, function(i) inherits(get(i,envir=envir),"data.frame"))
varNames[dfs]
}
testFun <- function(inData,sourceVar,targetVar,outData)
{
inData[[targetVar]] <- as.factor(inData[[sourceVar]])
assign(outData, inData, envir = .GlobalEnv)
}
lst <- list()
lst$action <- list(beginning="toFactor(",ending=")")
lst$arguments$inData <- list(type="gcombobox",lsDF())
lst$arguments$sourceVar <- list(type="gedit")
lst$arguments$targetVar <- list(type="gedit")
lst$arguments$outData <- list(type="gedit")
ggenericwidget(lst, container=gwindow("Recode to Factor"))
I wouldn't use ggenericwidget
(it isn't supported in gWidgets2). Instead create a combobox along the lines of cb <- gcombobox(character(0), cont=...)
then to populate it call its [<-
method: e.g., cb[] <- lsDf()
.
Here is a pattern for you to work with:
library(gWidgets2)
data(mtcars)
testdf <- data.frame(a=1:3, b= 1:3)
## list data frame names in envir
dfnms <- names(Filter(is.data.frame, mget(ls())) )
lsNms <- function(d, envir=.GlobalEnv) names(get(d, envir=envir))
w <- gwindow("two combos")
g <- glayout(cont=w)
g[1,1] <- "Data frames:"
g[1,2] <- (dfs <- gcombobox(dfnms, cont=g))
g[2,1] <- "Variables:"
g[2,2] <- (vnames <- gcombobox(character(0), cont=g))
addHandlerChanged(dfs, handler=function(...) vnames[] <- lsNms(svalue(dfs)))