0
votes

Does anyone have an idea how I would go about saving a datatable (DT::renderDataTable) output from shiny as a .png? I.e., I want to create a button, similar to this button?

E.g., could anyone define a button to save this table as a 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'))
      )
    })

```
1
would saving the table data to a tabular format not make sense for your application? I haven't seen many cases of saving a table as an image.. - DeanAttali
For inclusion into a static report where I do not want to load and wrangle the data.. - Nick
Look into here also, maybe you can just use DT library to do all your exporting stackoverflow.com/questions/24510671/… - Pork Chop

1 Answers

1
votes

Your main task is how to convert the table into PDF format. Here are two SO questions with answers about how to do that.

Suppose you figure that part out using those reference questions (this isn't shiny related). The way I would approach the next step is by first returning the path to the PDF you create from the function that makes the PDF

createPDF <- function(df) {
  # create a pdf
  return(pdf_file)
}

And then in Shiny you'd use a downloadHandler and you just need to copy that PDF into the filename you supply to downloadHandler. Something like this:

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

I haven't tried this but that's what I would try