【问题标题】:filter renderdatatable shiny过滤渲染数据表闪亮
【发布时间】:2017-05-05 21:38:23
【问题描述】:

我的闪亮服务器上有一个数据表,它在 server.R 中定义如下:

output$datamining_table <- DT::renderDataTable(datatable(values$datamining_list,selection="single",
                                                       option=list(lengthMenu = list(c(5, 10,20, -1), c('5', '10','20', 'All')),
                                                                   pageLength = 10,scrollX=T),filter = "top",rownames = F))

我的 ui.R 中还有一个带有按钮的 selectizeInput。当我选择我的 selectizeInput 的特定值并单击按钮时,我希望根据我的 selectizeInput 的值过滤下面的数据表。

例如:

数据表:

  • 名称1值A
  • 名称2值B
  • 名称1值C
  • 名称3值D

如果在 selectizeInput 中我选择 Name1,然后单击我的按钮,我希望我的数据表看起来像:

  • 名称1值A
  • 名称1值C

将第一列的过滤条件设置为 Name1,以便我可以取消过滤。 有人知道如何实现这一目标吗?

非常感谢您的帮助!

编辑: 这是一个可重现的代码:

    library(shiny)
library(DT)
runApp(list(

  ui=
    div(selectizeInput("selectInput","filter",c("a","b","c"),options=list(maxItems=1,create = F,onInitialize = I('function() { this.setValue(""); }'))),
    dataTableOutput("datamining_table")
      ),

  server=function(input, output, session) {
    values <- reactiveValues()
    values$datamining_list <- data.frame(name =c("a","b","c"),
                                         value = c(1,2,3))
    output$datamining_table <- DT::renderDataTable(datatable(values$datamining_list,selection="single",
                                                             option=list(lengthMenu = list(c(5, 10,20, -1), c('5', '10','20', 'All')),
                                                                         pageLength = 10,scrollX=T),filter = "top",rownames = F))
  }))

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这应该很有帮助。

        library(shiny)
        library(DT)
    
        ### User Interface
        ui <- shinyUI(fluidPage(
          mainPanel(
        fluidRow(
        selectizeInput("selectInput",label ="Filter", choices= NULL, selected = NULL)
        ),
        fluidRow(
        DT::dataTableOutput("datamining_table")
           )
         )
         )
        )
    
    
    server <- shinyServer(function(input, output,session){
    
      myDataFrame <- data.frame(name =c("a","b","c"),
                                value = c(1,2,3))
    
      updateSelectizeInput(session, 'selectInput', choices = c('a','b','c'), server = TRUE)
    
      filterData <- reactive({
          myDataFrame[which(myDataFrame$name == input$selectInput),]
    
      })
    
      output$datamining_table <- DT::renderDataTable({
        DT::datatable(filterData(),selection="single",rownames = F)
      })
    
    
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 嗨,非常感谢它几乎是完美的。它正确过滤了表格,但是无法删除特定名称上的过滤器(即使使用 filter="top",先前的值也不再存在。我会添加一个按钮来检索以前的值,然后我想。非常感谢!
    • @StaP:抱歉,我无法理解您的问题。如果要删除过滤器,可以使用返回箭头或delete键删除过滤器,如果要在selectizeInput中选择多个选项,可以在selectizeInput("selectInput"中添加参数"multiple=TRUE" ,....) 希望这就是你要找的。​​span>
    猜你喜欢
    • 2016-06-12
    • 2019-07-04
    • 2014-10-22
    • 2014-12-12
    • 2021-12-19
    • 2016-08-18
    • 2019-02-27
    • 2015-01-11
    • 2022-01-09
    相关资源
    最近更新 更多