Search code examples
rshinydt

R shiny datatables don't show records (rows) at start


I am building a shiny app with datatables. What I want is that there are no records (rows) shown on startup. So that you only see the filters at the top of the table. When you start typing the rows are shown. I can't find an option in Datatables. Is this possible?

An example code is here below.

    shinyApp(

ui = navbarPage(
    title = 'DataTable',
    DT::dataTableOutput('ex2')
),

server = function(input, output, session) {

    output$ex2 <- DT::renderDataTable(
        DT::datatable(
            iris, 
              options = list(
                dom = 'Bfrtip',
                lengthMenu = list(c(5, 15, -1), c('5', '15', 'All'))
            )
        )
    )

}
)

Solution

  • You could make your own search functionality:

    EDIT: added second character column that is being searched.

    shinyApp(
    
      ui = navbarPage(
        title = 'DataTable',
        textInput('search', 'search'),
        DT::dataTableOutput('ex2')
      ),
    
    
      server = function(input, output, session) {
    
        require(dplyr)
        iris.mut <- iris %>% 
          mutate(bottom = paste0('v',sapply(Sepal.Width,function(x)paste0(rep('z',x*2),collapse="")),'bx'))
    
        dat <- reactive({
    
          if(input$search!=''){
            iris.mut %>%
              filter(grepl(input$search,Species)|grepl(input$search,bottom))
          } else {
            iris.mut %>%
              filter(Species == input$search)
          }
        })
    
        output$ex2 <- DT::renderDataTable(
          DT::datatable(
            dat(), 
            options = list(
              lengthMenu = list(c(5, 15, -1), c('5', '15', 'All'))
            )
          ))
    
      }
    )