0

I’m trying to add a button to download the content of my DataTable as CSV. I want all the data available, not only the currently visible data in the table (columns can be hidden and the table is paginated).

Here’s the code for my table:

DT::renderDataTable({ 
# .../...
},
extensions = c('Buttons'),
options = list(
    lengthMenu = list(c(10, 100, 500, -1), c(10, 100, 500, "Tout")),
    pageLength = 100,
    searching = FALSE,
    language = list(url = "datatable.frFR.json"),
    dom = "<'row'<'col-sm-4 info'i><'col-sm-4 actions'B><'col-sm-4 filter'l>><'row'<'col-sm-12'tr>><'row'<'col-sm-5'i><'col-sm-7'p>>",
    buttons = list(
        list(extend = 'colvis', text = 'Colonnes affichées', columns = c(1:9)),
        list(extend = 'csv', text = 'Télécharger CSV', exportOptions = list(modifier = list(order = 'original', page = 'all')))
        ),
    columnDefs = list(
        list(targets = c(3, 5, 9), visible = FALSE)
    )
))

neither the order = 'original' nor the page = 'all' seem to be working (the current order is used and only the current number of rows is exported). What am I doing wrong?

gregseth
  • 12,952
  • 15
  • 63
  • 96

1 Answers1

0

server = FALSE option works in shiny.

ui <- fluidPage(
  DTOutput("tb1")
)

server <- function(input, output) {
  output$tb1 <- DT::renderDT({
    storms
    # .../...
  },
  server = FALSE,  ##  with this option all data is downloaded.  Without this only data in view is downloaded.
  extensions = c('Buttons'),
  options = list(
    lengthMenu = list(c(10, 100, 500, -1), c(10, 100, 500, "Tout")),
    pageLength = 100,
    searching = FALSE,
    language = list(url = "datatable.frFR.json"),
    dom = "<'row'<'col-sm-4 info'i><'col-sm-4 actions'B><'col-sm-4 filter'l>><'row'<'col-sm-12'tr>><'row'<'col-sm-5'i><'col-sm-7'p>>",
    buttons = list(
      list(extend = 'colvis', text = 'Colonnes affichées', columns = c(1:9)),
      list(extend = 'csv', text = 'Télécharger CSV', exportOptions = list(modifier = list(order = 'original', page = 'all')))
    ),
    columnDefs = list(
      list(targets = c(3, 5, 9), visible = FALSE)
    )
  ))

}

shinyApp(ui, server)
YBS
  • 19,324
  • 2
  • 9
  • 27