2 min read

Adding R Markdown documents of other output formats

View raw source for this post

This post is a minimal example to show how you can render arbitrary Rmd documents on a blogdown website.

Here are some possible use cases:

  • You have an existing Rmd file where the knitted .html is the version you would like to share on your website, with options (typically specified in your YAML frontmatter) not available with a blogdown post such as:

  • You have an existing Rmd file where the knitted PDF is the version you would like to share on your website. For example, a resume or CV, a course syllabus, etc.

  • You have an existing Rmd file where the knitted slide presentation is the version you want to share. See Carl’s issue here.

In all of the above use cases, the underlying “raw” Rmd documents will not render correctly using the blogdown::html_page() output format (see section 1.5 in the blogdown book). However, you can render Rmd files correctly in an output format other than blogdown::html_page() using blogdown. See the brief documentation in the blogdown book. The key is that you add an R script build.R under the R/ directory (in which you can use one line of code blogdown::build_dir('static')), and put your Rmd files under the static/ directory. Here are the four steps:

  1. Go to your blogdown project’s root directory and create a new folder called R
  2. In that R/ directory, create a new R script called build.R that contains 1 line of code that reads: blogdown::build_dir('static')
  3. Add and save Rmd file(s) to your blogdown project in the static/ directory.
    • In fact, you can add Rmd files within sub-directories such as static/slides/, static/pdf/, and/or static/html/
  4. Serve your site

It does not matter which output formats your Rmd files are generated to; build_dir() will call rmarkdown::render() to render them in the output format in the YAML header. So, Rmd files present in your static/ directory do not need to be knit to html in order to be rendered on your blogdown site. Any time you make changes to an Rmd file, the output file will be rendered again, overwriting the previous output file each time.

Please see the Github repo yihui/blogdown-static for a concrete example.