Exporting only Selected Rows from DT

£可爱£侵袭症+ 提交于 2019-12-11 05:59:00

问题


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.


回答1:


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))
        )
      )
    )
)




回答2:


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.



来源:https://stackoverflow.com/questions/56046948/exporting-only-selected-rows-from-dt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!