Sourcing file in RMD

Hello,

One of our applications has a couple of apps within it. In the parent directory, we have the app.R and a couple of folders. In one of the folders we have and RMD file that sources other files in other folders ( in child folders). There is a download button that suppose to download an html file produced by the RMD file. In the RMD file, we sources other files such as .RData which is in a child forlder.
The app works find and the download button does what it supposed to do and when we deploy the app to shinyserver it works fine too. However, when we deploy the app to R Studio Connect, the download button does not work.
One solution to the issue was to put all the files that are sourced in RMD in the parent directory and also saving the RMD to a temp file and then override it and use it. This ad hoc solution actually works! Here is a sample of saving it to a temp file,
content = function(file) {
src <- normalizePath('report.Rmd')

  # temporarily switch to the temp dir, in case you do not have write
  # permission to the current working directory
  owd <- setwd(tempdir())
  on.exit(setwd(owd))
  file.copy(src, 'report.Rmd', overwrite = TRUE)

  library(rmarkdown)
  out <- render('report.Rmd', switch(
    input$format,
    PDF = pdf_document(), HTML = html_document(), Word = word_document()
  ))
  file.rename(out, file)
}

This solution works but screw up other parts of the application. The question is, is there a way to fix the referencing (sourcing) files in RMD and be able to get the download button to work without making a major change to the application?

Best
Mo

Thanks for writing this up, Mo! Can you share a bit more detail about what happens here:

Is there an error message or log message that would give a bit more detail about why / how it is failing? For instance, does the Rmd reference the data using a relative path? If so it may be necessary to "change / set the working directory" before rendering the Rmd.

Also, do you have the tree command installed? I think tree ./ would help give us a picture of what the folder structure looks like here.

For example, with this folder structure:

./
├── app.R
├── data
│   └── myfile.rds
└── report
    └── myreport.Rmd

2 directories, 3 files

If myreport.Rmd uses ../data/myfile.rds to refer to the data file, then I will want to make sure that rmarkdown::render("report/myreport.Rmd") is running in the report folder. This seems to be the default behavior of knitr, but it is probably worth adding a log message inside myreport.Rmd that outputs getwd(), or specify the knit_root_dir argument to rmarkdown::render() when you render the file.

Does that help? I'm curious to see what error message you are getting when rendering your report!