3

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 (i.e. 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!!

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
Thomas Jensen
  • 860
  • 3
  • 11
  • 26
  • ??? How do you plan to subset on "female" when there is no such value in your data-object? – IRTFM Sep 07 '12 at 21:04
  • The gender variable is in the data, but I just used it as an example in the question. I imagine any example that shows how to filter with the posted subset can be easily adopted to also filter a given gender variable ;) – Thomas Jensen Sep 07 '12 at 21:10
  • You may wish to consider embedding your GUI within a larger framework such as Deducer or R Commander. You will get some of these data manipulation tasks for free. (e.g. http://www.deducer.org/pmwiki/pmwiki.php?n=Main.Subset ) – Ian Fellows Sep 08 '12 at 00:08

2 Answers2

2

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.

jverzani
  • 5,600
  • 2
  • 21
  • 17
0

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
IRTFM
  • 258,963
  • 21
  • 364
  • 487