Where do I save the Rmd file I am generating with a Shiny R App

I have an app to aloter the params argument in an Rmd file and then download said rmd file using the shiny app. Where do I save the rmd file and how do I tell shiny where to look?

This is my app:

setwd("C:/Users/JL16/Documents/Apps/qc-app")
library(shiny)
library(markdown)

 ui = fluidPage(
    textInput("year", "Enter Year"),
    downloadButton("report", "Generate report")
  )
 server <- function(input, output, session) {
   output$report <- downloadHandler(
     filename = "report.html",
     content = function(file) {
       params <- list(year = input$year)
       
       rmarkdown::render("report.Rmd", 
                         output_file = file,
                         params = params,
                         envir = new.env(parent = globalenv())
       )
     }
   )
 }
  
shinyApp(ui, server)

Maybe https://mastering-shiny.org/action-transfer.html#downloading-reports will help?

For the path you are using, the Rmd file should be in the root folder of your app (the same where the app.R file is located).

Doing this, especially on a Shiny app is a bad idea, the app is going to fail on deployment.
A better workflow would be to create a project for your app, that way you don't need to manually set the working directory.

This simple example works for me

library(shiny)
library(markdown)

ui = fluidPage(
  textInput("year", "Enter Year"),
  downloadButton("report", "Generate report")
)
server <- function(input, output, session) {
  output$report <- downloadHandler(
    filename = "report.html",
    content = function(file) {
      params <- list(year = input$year)
      
      rmarkdown::render("report.Rmd", 
                        output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
}

shinyApp(ui, server)

report.Rmd

---
title: "Test"
output: html_document
params:
  year: "2020"
---

```{r}
params$year
```

Rendered report

1 Like

Thanks for the response andresrcs. So when I click generate report it opens a file explorer and asks me to save a file called report but nothing is generated. In the console I get the following error:

Version:1.0 StartHTML:0000000107 EndHTML:0000001754 StartFragment:0000000127 EndFragment:0000001736

Listening on http://127.0.0.1:5494 Warning in normalizePath(path.expand(path), winslash, mustWork) : path[1]="report.Rmd": The system cannot find the file specified Warning: Error in abs_path: The file 'report.Rmd' does not exist. [No stack trace available]

Are you still manually setting the working directory? The issue is self explanatory, Shiny is not being able to find report.Rmd where you are saying it is.

No ive not manually set it this time, getwd() gives me:

"C:/Users/JL16/Documents"

This isn't where the app is stored, my understanding is that shiny should auto detect where my app.R file and report.Rmd file is.

Thanks J

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