Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny datatable pagination and show all rows as options

I have a datatable in a shiny application where I am doing pagination to show only 15 rows. But can I add an option where the user can see 15 rows at a time using pagination or a show all button which will show all the records with a scroll bar may be.

library(shiny)
library(DT)
library(shinyWidgets)
library(shiny)


shinyApp(

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

  server = function(input, output, session) {

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

    }
    )
like image 499
SNT Avatar asked Mar 13 '19 23:03

SNT


3 Answers

How about this, using the buttons extension. We define a custom button that calls the javascript function page.len(-1), where -1 means all rows:

shinyApp(

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

  server = function(input, output, session) {

    output$ex2 <- DT::renderDataTable(
      DT::datatable(
        iris, 
        extensions = 'Buttons',
        options = list(
          dom = 'Bfrtip',
          lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
          pageLength = 15,
          buttons = list(
            list(
              extend = "collection",
              text = 'Show All',
              action = DT::JS("function ( e, dt, node, config ) {
                                    dt.page.len(-1);
                                    dt.ajax.reload();
                                }")
            )
          )
        )
      )
    )

  }
)
like image 61
dww Avatar answered Nov 15 '22 06:11

dww


library(dplyr)
library(shiny)
library(DT)

shinyApp(

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

  server = function(input, output, session) {

    output$ex2 <- DT::renderDataTable(
      DT::datatable(
        iris, 
        extensions = 'Buttons',
        options = list(
          dom = 'tpB',
          lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
          pageLength = 15,
          buttons = list(
            list(
              extend = "collection",
              text = 'Show All',
              action = DT::JS("function ( e, dt, node, config ) {
                              dt.page.len(-1);
                              dt.ajax.reload();}")
            ),list(
              extend = "collection",
              text = 'Show Less',
              action = DT::JS("function ( e, dt, node, config ) {
                              dt.page.len(10);
                              dt.ajax.reload();}")

              )
              )
              )
            )
          )

  }
      )
like image 36
SNT Avatar answered Nov 15 '22 07:11

SNT


Set the dom = "ft" in the options of renderDataTable. Here are all the dom options. Basically this is only enabling "f - filtering" and "t - table". The "p-pagination" is missing. Then set the pageLength to be displayed to something really big (10000 rows in this example)*. Below is minimal example based on your code:

library(shiny)

ui <- fluidPage(
  DT::dataTableOutput('my_table')
)

server <- function(input, output) {
  
  output$my_table <- DT::renderDataTable(
    iris, 
    options = list(dom = "ft",
                   pageLength = 10000)
  )
}

shinyApp(ui = ui, server = server)

*Better yet, make the pageLength dynamic based on the size of your table.

like image 45
Jeff Parker Avatar answered Nov 15 '22 06:11

Jeff Parker