首页 > 解决方案 > R Datatables 自定义按钮

问题描述

我对 R 中的 DT 包比较陌生,但我正在尝试在单击按钮时自定义 CSV 文件的输出。我一直在关注这些示例中的最后一个,但我的代码不起作用。这是我的 .Rmd 文件:

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DT)
```


```{r results='hide'}
dt <-
  datatable(iris,
            extensions = 'Buttons',
            options = list(pageLength = 5, dom = 'Blfrtip', buttons = list(
              list(
                extend = 'csv',
                text = 'Create Custom CSV',
                action = DT::JS(
                  "function (e, dt, button, config) {
                  // Eventually process some data
                  $.fn.dataTable.ext.buttons.csvHtml5.action.call(this, e, dt, node, config);
                  }"
                )
              )
            )))
```

`r dt`

这个想法是我对数据进行一些处理,然后简单地调用默认的 csvHtml5 操作。在我的示例中,我只是希望返回默认的 csv 操作,但是当我单击针织 HTML 文件中的“创建自定义 CSV”按钮时,没有任何反应。

标签: rdatatablesdt

解决方案


你必须更换

function (e, dt, button, config)

function (e, dt, node, config)

推荐阅读