Rendering files in temporary environments inside Shiny applications

I'm trying to build a Shiny app which allows users to generate parameterised .Rmd documents based on their inputs. To generate the report, I first copy the report file to a temporary directory, before rendering the file in a new environment. My issue is that my Rmd document relies on other files - images and an .Rda file, which cannot be accessed in these empty environments and I have not found a way to copy them to the temporary directory or new environment. I need these files to be available when the .Rmd document is rendered with render(). Any suggestions?


 output$generate <- downloadHandler(filename = "examplereport.html",
    content = function(file) {
     tempReport <- file.path(tempdir(), "report.Rmd")
     file.copy("examplereport.Rmd", tempReport, overwrite = TRUE)
  
     params <- list(parameter1 = input$1, parameter2 = input$2, parameter3 = input$3)
      rmarkdown::render(tempReport, 
        output_file = file,
                      params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )```

Wouldn't it work to create the tempdir and copy the required files?

my_tempdir <- tempdir()

path_report <- paste0(my_tempdir, "/examplereport.Rmd")
path_rda <- paste0(my_tempdir, "/file_we_need.rda")

file.copy("examplereport.Rmd", path_report)
file.copy("templates/file_we_need.rda", path_rda)

initial_dir <- getwd()
setwd(my_tempdir)

rmarkdown::render(path_report, 
        output_file = file,
                      params = params,
                        envir = new.env(parent = globalenv())

setwd(initial_dir)

And same way, you could define the environment beforehand, manipulate it as needed, then pass it to render().

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.