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
)
)
)
}
)
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();
}")
)
)
)
)
)
}
)
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();}")
)
)
)
)
)
}
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With