emilliman5
emilliman5

Reputation: 5956

Saving rmarkdown output locally from Shiny

I have a shiny app that generates a downloadable report using rmarkdown, which works fine. I would like a copy of the report to be saved on the shiny host each time a user uploads data to generate a report. Ideally, I'd like to do this from within the downloadHandler call so that I do not have to generate the report twice.

A minimal example (adapted from this shiny article):

library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf"
      filename = "report.html",
      content = function(file) {
        # Copy the report file to a temporary directory before processing it, in
        # case we don't have write permissions to the current working dir (which
        # can happen when deployed).
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        # Set up parameters to pass to Rmd document
        params <- list(n = input$slider)

        # Knit the document, passing in the `params` list, and eval it in a
        # child of the global environment (this isolates the code in the document
        # from the code in this app).
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

The rmarkdown file

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---

  ```{r}
# The `params` object is available in the document.
params$n
```

A plot of `params$n` random points.

```{r}
plot(rnorm(params$n), rnorm(params$n))
```

Upvotes: 0

Views: 1037

Answers (1)

Gregor de Cillia
Gregor de Cillia

Reputation: 7655

You basically just need to copy the generated report into another directory. Here is an example based on the code you gave.

library(shiny)

if(!dir.exists("reportDir"))
   dir.create("reportDir")

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    downloadButton("report", "Generate report")
  ),
  server = function(input, output) {
    output$report <- downloadHandler(
      # For PDF output, change this to "report.pdf"
      filename = "report.html",
      content = function(file) {
        # Copy the report file to a temporary directory before processing it, in
        # case we don't have write permissions to the current working dir (which
        # can happen when deployed).
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        # Set up parameters to pass to Rmd document
        params <- list(n = input$slider)

        # Knit the document, passing in the `params` list, and eval it in a
        # child of the global environment (this isolates the code in the document
        # from the code in this app).
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )

        # copy generated report
        file.copy(file, paste("reportDir/", Sys.time(), ".html"))
      }
    )
  }
)

Upvotes: 1

Related Questions