25

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.

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)

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

Do you have any idea of what I am doing wrong? Thanks for your help

zx8754
  • 52,746
  • 12
  • 114
  • 209
Remi
  • 961
  • 1
  • 13
  • 25
  • 2
    where is the `extensions = 'Buttons'` command in your `datatable()`? – Stephan Apr 26 '18 at 09:19
  • 1
    Thanks, buttons now appear, but all renderDataTable features have dissapeared (column filter, search box, row selection, pagination, etc)... Do you have any idea on how to make them re-appear? – Remi Apr 26 '18 at 09:28
  • check that [table](https://datatables.net/download/compatibility) hope your features are in a green box. – Stephan Apr 26 '18 at 09:30
  • I am using Firefox Quantum 59.0.2 (64 bits). When I use only renderDataTable everything works fine (but of course, buttons are not there) output$mytable1 = DT::renderDataTable({ plots.dfs()[[1]] }) – Remi Apr 26 '18 at 09:32
  • so without the `extensions` argument, all features are working? – Stephan Apr 26 '18 at 09:36
  • No. When I use datatable() inside renderDataTable(), the only feature that work is buttons. All other features have dissapeared (column filter, search box, row selection, pagination, etc) – Remi Apr 26 '18 at 09:39
  • @Stephan : As the first question (add a download button) has been answered, I have created a new question for pagination and search box : https://stackoverflow.com/questions/50043152/r-shiny-how-to-add-pagination-in-dtrenderdatatable – Remi Apr 26 '18 at 12:44

3 Answers3

30

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"
                       ))
Remi
  • 961
  • 1
  • 13
  • 25
  • 3
    Thanks, that works just fine, but is there an option to download all data (inside other pagings) instead of 'only' the data shown? – Bernardo May 31 '19 at 17:26
  • 3
    The method has been to add `server = FALSE` to `renderDataTable` to allow it to fetch the whole table instead of just the part displayed, but now there are also functions in Shiny to add download buttons. See [here](https://shiny.rstudio.com/reference/shiny/1.0.4/downloadButton.html), [here](https://community.rstudio.com/t/r-shiny-to-download-xlsx-file/18441), and [here](https://github.com/rstudio/DT/issues/267) for more about this. – Brian Stamper Dec 31 '19 at 16:34
10

Adding one more solution to keep search and paging (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) ))
Ricky
  • 1,005
  • 1
  • 12
  • 15
5

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. You can make the buttons download the entire dataset with renderDT(server = FALSE, {}) as used below:

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')))
})
Ricky
  • 1,005
  • 1
  • 12
  • 15
  • 1
    This approach disables the search bar and page filtering due to `dom='tB'`. Do you know of a way to recover this using this approach? – bmc Mar 26 '21 at 14:07
  • sending the whole table to the browser can cause it to freeze in case of very large tables – Antoine Dec 09 '22 at 21:30