【问题标题】:Hide a column in shiny datatable but keep it searchable隐藏闪亮数据表中的列,但保持可搜索
【发布时间】:2020-12-04 15:17:45
【问题描述】:

Shiny 中的 DT 包生成一个带有搜索栏的表格,用于搜索表格中的每一列。我有一列不想在表格中显示的元数据,但如果我使用搜索栏进行搜索,我仍然希望这些行出现。

例如,下面的应用程序包含一个标题为searchCol 的列。这个专栏只是字母。我想在实际表格中隐藏此列,并且希望能够使用 DT 搜索栏搜索字母 b 并显示第二行。

有没有办法隐藏该列但它仍然可以与搜索栏一起使用?

library(shiny)
library(DT)
ui <- fluidPage(
    DTOutput('tbl1'),
)
server <- function(input, output, session) {
    output$tbl1 <- DT::renderDT(server = TRUE, {
        datatable(
            cbind(data.frame(replicate(3,sample(0:1,26,rep=TRUE))), data.frame(searchCol = letters)),
            escape = FALSE,
            rownames = FALSE, 
            filter = list(position = "top", clear = FALSE, plain = TRUE),             
            selection = "single",
            options = list(
                autoWidth = TRUE,
                pageLength = 50,
                lengthMenu = c(50, 100, 1000), 
                dom = 'Blfrtip',             
                buttons = c('copy', 'excel')
            )
        )
    })
}
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny datatables


    【解决方案1】:

    我已将here 的答案调整为您需要在DT::datatable 中使用的格式。您可以使用columnDefs 定义不同列的渲染选项,targets 定义您所指的列。请注意,JS 库datatables 从 0 开始计数列。

    library(shiny)
    library(DT)
    ui <- fluidPage(
      DTOutput('tbl1'),
    )
    server <- function(input, output, session) {
      output$tbl1 <- DT::renderDT(server = TRUE, {
        datatable(
          cbind(data.frame(replicate(3,sample(0:1,26,rep=TRUE))), data.frame(searchCol = letters)),
          escape = FALSE,
          rownames = FALSE, 
          filter = list(position = "top", clear = FALSE, plain = TRUE),             
          selection = "single",
          options = list(
            autoWidth = TRUE,
            pageLength = 50,
            lengthMenu = c(50, 100, 1000), 
            dom = 'Blfrtip',             
            buttons = c('copy', 'excel'),
            columnDefs = list(
              list(
                targets = 3,
                searchable = TRUE,
                visible = FALSE
              )
            )
          )
        )
      })
    }
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 2012-10-28
      • 1970-01-01
      • 2015-09-22
      • 2022-01-06
      • 1970-01-01
      • 2020-07-27
      • 2021-06-21
      相关资源
      最近更新 更多