Is there a way to have a user upload an image in an R Shiny app and have it render in a downloadable markdown report?

Viewed 50

I have a shiny app that currently takes user input and renders it into a downloadable markdown report. I'm getting stuck on having the user upload an image and having it render properly on the downloaded report. I know how to embed an image that's locally saved into a markdown report but what about one that gets uploaded? Can the user upload several images to render as well? I basically just need pointed in the right direction as I haven't been able to find anything about whether or not this is possible.

1 Answers

without a reprex it is hard to answer your questions but I assume you have this setup as a parameterised report as per the instructions here. https://shiny.rstudio.com/articles/generating-reports.html

I would suggest setting up this image as a paramater that is passed to the r markdown YAML header, would look like the following.

In your app somepicture would be added to the parameters.

    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 = 'somepicture')

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

In your markdown make sure it's included in the YAML header and call for it with params$somepicture

like the following, sorry I do not know of a function to use in the markdown off the top of my head.

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

```{r}
some_function_to_add_a_picture(params$n)


Related