0

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

```
Community
  • 1
  • 1
Nick
  • 3,262
  • 30
  • 44
  • 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 Feb 11 '16 at 10:36
  • For inclusion into a static report where I do not want to load and wrangle the data.. – Nick Feb 11 '16 at 11:03
  • Look into here also, maybe you can just use `DT` library to do all your exporting http://stackoverflow.com/questions/24510671/r-shiny-datatables-with-tabletools-and-other-extensions – Pork Chop Feb 11 '16 at 11:15

1 Answers1

1

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

Community
  • 1
  • 1
DeanAttali
  • 25,268
  • 10
  • 92
  • 118