闪亮的数据表:使用按钮扩展名保存完整的data.frame [英] Shiny DataTable: Save full data.frame with buttons extension

查看:55
本文介绍了闪亮的数据表:使用按钮扩展名保存完整的data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将DataTables与Shiny一起使用。使用按钮扩展名,用户可以下载或打印数据表中的数据。但是仅下载/打印行的可见部分。我想更改该行为,以便可以下载包含所有行的完整data.frame。

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)


推荐答案

如果您使用 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')
                                      ))  




编辑


正如@Jav所评论的那样,当您拥有大型数据集时,此解决方案将不起作用。 @Jav指出,使用 server = FALSE 可能是更好的解决方法,它允许您使用分页或滚动模式:


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

如果您有一个非常大的数据集,但您不想一开始就完全加载,则应该实现Shiny的下载处理程序。

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.

这篇关于闪亮的数据表:使用按钮扩展名保存完整的data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆