I am building a custom GUI in R for work, and I need to have a part that can select a subset of a dataframe based on variable values (ie select all females that are above 50 etc.). I am building the GUI with gwidgets, but I am stuck with regards to how this filter can be implemented. Specifically how to create a widget that allows the user to select one or more filters and then return the filtered data frame.
Here is a small sample from the data I am working with:
structure(list(kunde = c(3, 3, 3, 3, 3, 3, 3, 1, 3, 3),
bank = c(7,98, 3, 3, 98, 2, 2, 1, 7, 2)),
.Names = c("kunde", "bank"), row.names = c(NA, 10L), class = "data.frame")
Any help is greatly appreciated!!
There are some examples of similar things in the ProgGUIinR
package. Here is one of them:
library(gWidgets)
options(guiToolkit="RGtk2")
options(repos="http://streaming.stat.iastate.edu/CRAN")
d <- available.packages() # pick a cran site
w <- gwindow("test of filter")
g <- ggroup(cont=w, horizontal=FALSE)
ed <- gedit("", cont=g)
tbl <- gtable(d, cont=g, filter.FUN="manual", expand=TRUE)
ourMatch <- function(curVal, vals) {
grepl(curVal, vals)
}
id <- addHandlerKeystroke(ed, handler=function(h, ...) {
vals <- tbl[, 1, drop=TRUE]
curVal <- svalue(h$obj)
vis <- ourMatch(curVal, vals)
visible(tbl) <- vis
})
For your purpose, you might want to use gcheckboxgroup
or gcombobox
to select factor levels or a level and filter by that. The key is the visible<-
method of the gtable
object is used to filter the displayed items.
If you are game, you can try the gfilter
widget in gWidgets2
which as of know is just on my github site (use install_packages("gWidgets2", "jverzani")
from devtools, also gWidgets2RGtk2
). This may be just what you are trying to do.
With your data object and testing one of the variables, this is a simplified version of subset.data.frame
:
tmp <-
structure(list(kunde = c(3, 3, 3, 3, 3, 3, 3, 1, 3, 3), bank = c(7,
98, 3, 3, 98, 2, 2, 1, 7, 2)), .Names = c("kunde", "bank"), row.names = c(NA,
10L), class = "data.frame")
getsub <- function(obj, logexpr) if (missing(logexpr)) {return(obj)
} else {e <- substitute(logexpr)
r <- eval(e, obj, parent.frame())
if (!is.logical(r))
stop("'subset' must evaluate to logical")
r <- r & !is.na(r)
obj[r, ] }
getsub(tmp, bank <50)
#--------------
kunde bank
1 3 7
3 3 3
4 3 3
6 3 2
7 3 2
8 1 1
9 3 7
10 3 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.