Shiny DataTable: Save full data.frame with buttons extension

牧云@^-^@ 提交于 2019-12-22 05:13:36

问题


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)

回答1:


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.




回答2:


If you use server=FALSE,when the data is big,it will be disconnect



来源:https://stackoverflow.com/questions/41729259/shiny-datatable-save-full-data-frame-with-buttons-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!