Is it possible to save the HTML output in a directory which is not the one where the .Rmd file resides

Working in a Windows enviroment, I'm not a big user of the Command Line, but a CLI solution is always good to know. I may switch to a Mac in the future, in which case I'll use the CLI much more. It's the make-based solution @mara was referring to. Thanks for sharing an implementation of this approach!

BTW, the GitHub workflow you were referring to is exactly why all this started - a coworker of mine was used to finding HTMLs in the /docs folder and he was surprised they weren't there :grinning:

2 Likes

For reference, here is my simple 5-minutes-to-Makefile example/tutorial:

1 Like

If you have a collection of R Markdown files that you want to save in a specific output directory, e.g. docs/ to serve on GitHub Pages, I'd recommend trying the functionality for R Markdown websites. Save the file _site.yml in the same directory as the R Markdown files (e.g. code/ in the initial post) with the following entry:

output_dir: ../docs

And name at least one of the R Markdown files index.Rmd. Then call render_site instead of render to create the HTML. This has other nice features:

  1. Automatically makes the documents not self-contained. This way they share the Javascript, CSS, etc. instead of embedding this into each HTML file.
  2. It works with RStudio. If you click on the Knit button, RStudio recognizes that there is a _site.yml file and an index.Rmd file and automatically runs render_site (and thus your files are written to ../docs without having to run an external R script or Makefile).
  3. You can share other options across the R Markdown files, e.g. the output settings or a navigation bar.
8 Likes

I have only one R Markdown for project, but your solution is still pretty nice, and works in RStudio! The only nag is that the content of the _site.yml file depends on the OS (because different OS use different separators for folders), while a solution based on file.path would run as is even if a coworker of mine with a different OS would just clone my repo. Still, it's a very small and contained dependency (1 line of 1 file). I could test it...thanks!

1 Like

@Andrea You don't have to worry about this. As long as you use a forward slash as the separator, e.g. output_dir: ../output, this will work across Linux, macOS, and Windows. If your coworkers like to use spaces in their directory names, then you would need your team to get in the habit of surrounding the path in quotation marks.

1 Like

@jdblischak ok! I didn't know that. About spaces in the directory names, my coworkers know about my aversion for folder names with spaces, so they don't clone my projects in such ill-formed paths :stuck_out_tongue_winking_eye:

1 Like