将 renderDataTable 输出保存为来自闪亮的 png 的按钮

Button which saves renderDataTable output as png from shiny

有谁知道我如何将 shiny 的数据table (DT::renderDataTable) 输出保存为 .png?即,我想创建一个按钮,类似于 this button?

例如,任何人都可以定义一个按钮来将此 table 另存为 png:

---
output: html_document
runtime: shiny
---

```{r setup, echo=FALSE}
library(DT)

    DT::renderDataTable({
      datatable(iris) %>% formatStyle(
        'Sepal.Width',
        backgroundColor = styleInterval(3.4, c('gray', 'yellow'))
      )
    })

```

您的主要任务是如何将 table 转换为 PDF 格式。 Here are two SO questions 以及如何做到这一点的答案。

假设您使用这些参考问题计算出该部分(这与闪亮无关)。我下一步的方法是首先返回您从制作 PDF

的函数创建的 PDF 的路径
createPDF <- function(df) {
  # create a pdf
  return(pdf_file)
}

然后在 Shiny 中,您将使用 downloadHandler,您只需将该 PDF 复制到您提供给 downloadHandler 的文件名中。像这样:

### in UI
downloadButton('downloadPdf', 'Download table')

### in server
output$downloadPdf <- downloadHandler(
    filename = function() {
      "table.pdf"
    },
    content = function(file) {
      file <- createPDF(df)
      file.copy(file, "table.pdf")  # or something like this
    }
)

我没试过这个,但我会尝试这个