4
votes

I have a datatable on my shiny server that is defined in server.R as follows:

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))

I also have a selectizeInput in my ui.R with a button. When I select a specific value of my selectizeInput and click on the button, I would like the datatable below to be filtered on the values of my selectizeInput.

For example:

DataTable:

  • Name1 valueA
  • Name2 valueB
  • Name1 valueC
  • Name3 valueD

If in the selectizeInput I select Name1 and then click on my button, I would like my datatable to look like:

  • Name1 valueA
  • Name1 valueC

with the filter criteria of the first column set to Name1 so that I can unfilter it. Would someone have an idea of how to achieve this?

Thanks a lot for your help!

EDIT: Here is a reproducible code:

    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))
  }))
1

1 Answers

3
votes

This should be helpful.

    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)