3

I'm using R's DT in a FlexDashboard. I have the export buttons working, but I'd like to be able to make the exports export only the data that is either selected via rows or when using the DT search function.

I've looked at the DT manual, but it hasn't clarified how I'd go about it.

datatable(
  dept_table, 
  rownames = FALSE,
  extensions = "Buttons",
    options = 
    list(
      searching = TRUE, 
      pageLength = 200, 
      scrollX = TRUE,
      scrollY = TRUE,
      dom = "BRSpfrti",
      buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
)

Thus, if I have a table of 128 rows, and I use the search to select only 10, my export should only have those 10 rows.

DocProc
  • 103
  • 7

2 Answers2

3

This is possible, with the Select extension. Include this extension, set the option select = TRUE and set the buttons like this:

list(
  extend = "csv",
  text = 'CSV',
  exportOptions = list(modifier = list(selected = TRUE))
)

That is:

datatable(
  iris, 
  rownames = FALSE,
  extensions = c("Buttons", "Select"),
  options = 
    list(
      select = TRUE,
      searching = TRUE, 
      scrollX = TRUE,
      scrollY = TRUE,
      dom = "BRSpfrti",
      buttons = list(
        list(
          extend = "copy",
          text = 'Copy',
          exportOptions = list(modifier = list(selected = TRUE))
        ), 
        list(
          extend = "csv",
          text = 'CSV',
          exportOptions = list(modifier = list(selected = TRUE))
        ), 
        list(
          extend = "excel",
          text = 'Excel',
          exportOptions = list(modifier = list(selected = TRUE))
        ), 
        list(
          extend = "pdf",
          text = 'PDF',
          exportOptions = list(modifier = list(selected = TRUE))
        ), 
        list(
          extend = "print",
          text = 'Print',
          exportOptions = list(modifier = list(selected = TRUE))
        )
      )
    )
)

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
0

Maybe the best way is to add one or severals shiny button that will impact your dataframe before the DT visualisation (instead of using the filter directly on the table generated by the DT) and to export the whole dataset.

The filter you make with the search DT aren't store anywhere, so it is difficult then to export only the rows that corresponding with this research.

demarsylvain
  • 2,103
  • 2
  • 14
  • 33