I am using DataTables with Shiny. With the buttons extension a user can download or print the data in the datatable. But only the visible part of the rows is downloaded/printed. I want to change that behaviour, so that the full data.frame with all rows can be downloaded. Is this possible with the buttons extension or do I have to switch to a downloadHandler?
library(DT)
library(shiny)
df <- data.frame(a = 1:100, b = 1:100)
ui <- fluidPage(
dataTableOutput("table")
)
server <- function(input, output){
output$table <- DT::renderDataTable(df,
extensions = c("Buttons"),
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))
}
shinyApp(ui, server)
It would work if you use a Scroller
:
output$table <- DT::renderDataTable(df,
extensions = c('Buttons', 'Scroller'),
options = list(
dom = 'Bfrtip',
deferRender = TRUE,
scrollY = 400,
scroller = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))
As commented by @Jav, this solution doesn't work when you have a large dataset. @Jav pointed out that using server=FALSE
could be better a workaround, which allows you to use either the paging or scrolling mode:
output$table <- DT::renderDataTable(df, server = FALSE,
extensions = c("Buttons"),
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
))
If you have a very large dataset that you don't want to fully load at first then you should implement Shiny's download handler.
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