简体   繁体   English

在 DT::renderDataTable 中添加下载按钮

[英]Add download buttons in DT::renderDataTable

I am trying to add download buttons ('copy', 'csv', 'excel', 'pdf') above the table in my R Shiny app, but the renderDataTable seems doesn't work when using a datatable inside.我正在尝试在我的 R Shiny 应用程序的表格上方添加下载按钮('copy'、'csv'、'excel'、'pdf'),但是在内部使用数据表时,renderDataTable 似乎不起作用。

output$mytable1  <- DT::renderDataTable(
        datatable(
            { plots.dfs()[[1]] },
        rownames = TRUE,
        options = list(
            fixedColumns = TRUE,
            autoWidth = TRUE,
            ordering = FALSE,
            dom = 'tB',
            buttons = c('copy', 'csv', 'excel', 'pdf')
        ),
        class = "display"
    ))

When I use DT::renderDataTable without DT::datatable inside, renderDataTable works well and I have all features (filters, search field, etc), except download buttons (what I am trying to add)当我在没有 DT::datatable 的情况下使用 DT::renderDataTable 时,renderDataTable 运行良好并且我拥有所有功能(过滤器、搜索字段等),除了下载按钮(我要添加的内容)

output$mytable1 = DT::renderDataTable({ plots.dfs()[[1]] })

Do you have any idea of what I am doing wrong?你知道我做错了什么吗? Thanks for your help谢谢你的帮助

As Stephan said in comment, the way to add buttons is the following:正如斯蒂芬在评论中所说,添加按钮的方式如下:

output$mytable1  <- DT::renderDataTable(
                        DT::datatable(
                            { plots.dfs()[[1]] },

                            extensions = 'Buttons',

                            options = list(
                                paging = TRUE,
                                searching = TRUE,
                                fixedColumns = TRUE,
                                autoWidth = TRUE,
                                ordering = TRUE,
                                dom = 'tB',
                                buttons = c('copy', 'csv', 'excel')
                            ),

                            class = "display"
                       ))

When I stumbled on this solution it worked, but disabled search and paging.当我偶然发现这个解决方案时,它起作用了,但禁用了搜索和分页。 Adding one more solution of what ended up working for me (dom = 'Bfrtip'):添加一个最终对我有用的解决方案(dom = 'Bfrtip'):

datatable(data, extensions = "Buttons", 
            options = list(paging = TRUE,
                           scrollX=TRUE, 
                           searching = TRUE,
                           ordering = TRUE,
                           dom = 'Bfrtip',
                           buttons = c('copy', 'csv', 'excel', 'pdf'),
                           pageLength=5, 
                           lengthMenu=c(3,5,10) ))

Adding an answer that is more explicit about allowing to download the whole table since it should be more clearly outlined in my opinion.添加一个更明确的关于允许下载整个表格的答案,因为我认为应该更清楚地概述。 Using renderDT({}) the download buttons only download the data currently being displayed.使用renderDT({})下载按钮仅下载当前显示的数据。 You can make the buttons download the entire dataset with renderDT(server = FALSE, {}) as used below:您可以使用renderDT(server = FALSE, {})使按钮下载整个数据集,如下所示:

renderDT(server=FALSE,{
  # Load data
  data <- mtcars
  # Show data
  datatable(data, extensions = 'Buttons', 
            options = list(scrollX=TRUE, lengthMenu = c(5,10,15),
                           paging = TRUE, searching = TRUE,
                           fixedColumns = TRUE, autoWidth = TRUE,
                           ordering = TRUE, dom = 'tB',
                           buttons = c('copy', 'csv', 'excel','pdf')))
})

由于不允许发表评论,关于贝尔纳多的问题,一种下载所有数据的解决方案是在DT :: datatable中使用命令server ='False'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM