# NOT RUN {
## example to select CRAN mirror
m <- getCRANmirrors()[,c(1,4)]
setCRAN <- function(URL) { ## see chooseCRANmirror
repos = getOption("repos")
repos["CRAN"] <- gsub("/$", "", URL)
options(repos=repos)
}
w <- gwindow("gtable example",width=400)
gp <- ggroup(horizontal=FALSE, container=w)
tab <- gtable(m, chosencol = 2, container=gp, expand=TRUE,
handler = function(h,...) setCRAN(svalue(h$obj)))
bg <- ggroup(container=gp)
addSpring(bg)
gbutton("dismiss", container=bg, handler = function(h,...) dispose(w))
## an example with icons.
## Select variables from a data frame
## find icons by class
icon.FUN <- function(items) {
dfName <- svalue(cb)
df <- try(get(dfName, envir=.GlobalEnv), silent=TRUE)
if(inherits(df,"try-error"))
return(rep(NULL,dim(items)[1]))
if(is.data.frame(items))
items <- items[,1, drop=TRUE]
sapply(items, function(i) {
class(df[,i])[1]
})
}
## 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]
}
## set up GUI
w <- gwindow("Select variables",width=250)
g <- ggroup(horizontal=FALSE, container=w)
l <- glabel("Data frame", container=g)
cb <- gcombobox(lsDF(), container=g)
blankDF = data.frame(variables=character(0), stringsAsFactors=FALSE)
tbl <- gtable(blankDF, icon.FUN=icon.FUN, container=g, expand=TRUE)
## add handlers
addHandlerChanged(cb, handler <- function(h,...) {
dfName <- svalue(h$obj)
dfNames <- names(get(dfName,envir=.GlobalEnv))
tbl[,] <- data.frame(variables=dfNames, stringsAsFactors=FALSE)
})
addHandlerClicked(tbl, handler = function(h,...) {
cat("Do something with",svalue(cb),"::", svalue(h$obj),"\n")
})
# }
Run the code above in your browser using DataLab