调用DT数据表中的过滤条件

问题描述 投票:0回答:1

我正在构建一个使用

DT::renderDataTable()
的闪亮应用程序。它提供了一个优雅的面板,用于可视化和过滤数据集。

我知道

DT::renderDataTable
会自动将过滤后的数据集传递到进一步的操作。但出于某种原因,现在我还需要动态调用这些过滤条件。我所说的“调用过滤条件”是指提取该条件的精确表达式。例如,如果我将变量
x
的过滤更改为在
(-1, 1)
范围内,我想输出
(x > -1) & (x < 1)
的表达式或类似的内容。

有人知道如何实现这一目标吗?

r shiny dt
1个回答
1
投票

您可以使用

input$<tableId>_search_columns
访问搜索字符串。 他们使用自定义格式(例如,(-1, 1) 的范围为
"-1 ... 1"
), 并且 DT 中没有公开的 API 用于从搜索中解析 R 表达式 字符串,所以你需要自己编写一个解析器。

这里有一个可以帮助您入门的功能:

parse_search_expressions <- function(data, search) {
  parse_search_expression <- function(x, s) {
    if (!nzchar(s)) return(TRUE)
    if (is.numeric(x)) {
      r <- strsplit(s, "...", fixed = TRUE)
      r <- sapply(r, as.numeric)
      bquote((x >= .(r[1])) & (x <= .(r[2])))
    } else if (is.factor(x) || is.logical(x)) {
      v <- jsonlite::fromJSON(s)
      bquote(x %in% .(v))
    } else {
      bquote(grepl(.(s), x, fixed = TRUE))
    }
  }
  Map(parse_search_expression, data, search)
}

tbl <- data.frame(x = 1:3, y = factor(LETTERS[1:3]), z = c("foo", "bar", "baz"))
str(parse_search_expressions(tbl, c("1 ... 2", "[\"A\"]", "ba")))
#> List of 3
#>  $ x: language (x >= 1) & (x <= 2)
#>  $ y: language x %in% "A"
#>  $ z: language grepl("ba", x, fixed = TRUE)

如果你只是想应用过滤器,有

doColumnSearch()
(添加在 DT 0.22),这将让您获得与搜索字符串匹配的索引:

str(Map(DT::doColumnSearch, tbl, c("1 ... 2", "[\"A\"]", "ba")))
#> List of 3
#>  $ x: int [1:2] 1 2
#>  $ y: int 1
#>  $ z: int [1:2] 2 3

如果足够的话我会推荐后者,因为你保证 得到与DT过滤相同的结果。通过第一种方法,你有 复制过滤逻辑,由你来确保结果 是一样的。

最后,以下是如何在应用程序中使用这两种方法:

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("table"),
  verbatimTextOutput("expression"),
  verbatimTextOutput("search_result")
)

server <- function(input, output, session) {
  output$table <- renderDT(iris, filter = "top")
  output$expression <- renderPrint({
    str(parse_search_expressions(iris, input$table_search_columns))
  })
  output$search_result <- renderPrint({
    str(Map(doColumnSearch, iris, input$table_search_columns))
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.