简体   繁体   English

DT SearchPanes 自定义过滤器

[英]DT SearchPanes Custom Filter

I'm trying to do something like is seen here , but I'm having trouble figuring out how to do it in Shiny.我正在尝试做类似 is seen here 的事情,但我无法弄清楚如何在 Shiny 中做到这一点。 As an example, it would be great to have a filter for mtcars of "efficient" (cars with at least 15 mpg) or "inefficient" (cars with less than 15 mpg).例如,为“高效”(至少 15 mpg 的汽车)或“低效”(低于 15 mpg 的汽车)的mtcars设置过滤器会很棒。

Here is some code:这是一些代码:

library(shiny)
library(DT)

ui <- shinyUI(
  fluidPage(DT::dataTableOutput("mtcars_table"))
)

server <- shinyServer(function(input, output, session) {
  output$mtcars_table <- 
    DT::renderDT({
      DT::datatable(
        mtcars,
        options = list(dom = 'Pfrtip', 
                       columnDefs = list(
                         list(
                           searchPanes = list(show = TRUE), targets = 1
                         ),
                         list(
                           searchPanes = list(show = FALSE), targets = 2:11
                         ))),
        extensions = c('Select', 'SearchPanes'),
        selection = 'none'
      )
      
    }, server = FALSE)
})

shinyApp(ui = ui, server = server)

Here is something to try based on the DataTables example with custom filtering options.以下是基于带有自定义过滤选项的DataTables 示例尝试的内容。

For the additional list options, I included a label like "Efficient", as well as a javascript function for value ( rowData[1] should reference the first column, mpg ).对于其他list选项,我包含了一个像“Efficient”这样的标签,以及一个用于value的 javascript 函数( rowData[1]应该引用第一列mpg )。

library(shiny)
library(DT)

ui <- shinyUI(
  fluidPage(DT::dataTableOutput("mtcars_table"))
)

server <- shinyServer(function(input, output, session) {
  output$mtcars_table <- 
    DT::renderDT({
      DT::datatable(
        mtcars,
        options = list(
          dom = 'Pfrtip', 
          columnDefs = list(
            list(
              searchPanes = list(
                show = TRUE,
                options = list(
                  list(
                    label = "Efficient",
                    value = JS(
                      "function(rowData, rowIdx) { return rowData[1] >= 15; }"
                    )
                  ),
                  list(
                    label = "Inefficient",
                    value = JS(
                      "function(rowData, rowIdx) { return rowData[1] < 15; }"
                    )
                  )
                )
              ),
              targets = 1
            ),
            list(
              searchPanes = list(show = FALSE), targets = 2:11
            )
          )
        ),
        extensions = c('Select', 'SearchPanes'),
        selection = 'none'
      )
    }, server = FALSE)
})

shinyApp(ui = ui, server = server)

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

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