Better Organization of RMarkdown Outputs

By default, knit renders RMarkdown outputs to the same folder of the .Rmd file. The RMarkdown Cookbook demonstrates that the behavior of the Knit button can be changed by defining the custom knit function in the YAML frontmatter.

Based on the technique, this post will show you how to improve the organization of the RMarkdown outputs in your project.

Suppose your project is organized with the following folder structure:

Because the R Notebook / RMarkdown outputs are saved in the same folder as the source code files by default, it makes the R code folder cluttered with too many files. Also, having all the outputs under the same name is not good for versioning. Sometimes we want the outputs to be dated. So ideally, we want the files to be organized like this:

To achieve this, we need to do three things to the RMarkdown output:

  • Redirect all the outputs to the “R Notebook” folder.
  • Replicate the subfolders from the “R Code” folder to the “R Notebook” folder.
  • Append date to the end of the filename.

We can get all these done in a few lines:

To use this custom function in knit, just put it in “Include.R” and then call it from the YAML frontmatter:

Leave a Reply