简体   繁体   English

GUI用于在R中对数据帧进行子集化

[英]GUI to subset a data frame in R

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.). 我正在R中构建一个自定义GUI用于工作,我需要有一个部分可以根据变量值选择数据帧的子集(即选择50以上的所有女性等)。 I am building the GUI with gwidgets, but I am stuck with regards to how this filter can be implemented. 我正在使用gwidgets构建GUI,但我对如何实现此过滤器感到困惑。 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. ProgGUIinR包中有一些类似的例子。 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. 出于您的目的,您可能希望使用gcheckboxgroupgcombobox来选择因子级别或级别并gcombobox过滤。 The key is the visible<- method of the gtable object is used to filter the displayed items. 关键是visible<- gtable对象的方法用于过滤显示的项目。

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 ). 如果你是游戏,你可以尝试gfilter窗口小部件gWidgets2 (使用其作为专门的只是在我的github网站install_packages("gWidgets2", "jverzani")从devtools,也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 : 使用您的数据对象并测试其中一个变量,这是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM