仅从DT导出选定的行 [英] Exporting only Selected Rows from DT

查看:96
本文介绍了仅从DT导出选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在FlexDashboard中使用R的DT。我可以使用导出按钮,但是我希望能够使导出仅导出通过行或使用DT搜索功能选择的数据。

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.

我看过DT手册,但并没有弄清楚我该怎么做。

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

因此,如果我有128表行,而我使用搜索只选择了10行,我的导出应该只包含那10行。

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.

推荐答案

扩展名包括 Select 并设置选项 select = TRUE 并设置如下按钮:

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

即:

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

这篇关于仅从DT导出选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆