Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny DataTable: Save full data.frame with buttons extension

Tags:

r

dt

shiny

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)
like image 589
needRhelp Avatar asked Jan 18 '17 20:01

needRhelp


1 Answers

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

Edit

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.

like image 163
HubertL Avatar answered Oct 17 '22 10:10

HubertL