Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add download buttons in DT::renderDataTable

Tags:

r

dt

shiny

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

like image 787
Remi Avatar asked Apr 26 '18 09:04

Remi


2 Answers

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"
                       ))
like image 185
Remi Avatar answered Oct 23 '22 10:10

Remi


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')))
})
like image 44
Ricky Avatar answered Oct 23 '22 12:10

Ricky