1 Purpose

This document describes how to create ‘child’ R Markdown documents that are incorporated into a parent document during the rendering process.

For example, Rick uses this to generate a full syllabus from the various parts of the course website (Rmd | docx | PDF).

You could use this to create separate R Markdown documents for different plots, or sections of a bigger piece of work.

2 Procedure

  • Create a parent R Markdown document as you normally would.
  • Create one or more “child” R Markdown documents.
    • Make sure to delete the default setup chunk, or you will get an error. There can’t be more than one chunk with the same name in your (combined) document.
    • The “child” documents can have minimal YAML header info, a pagetitle: parameter is all that is needed:
---
pagetitle: "my-child"
---
  • Place a chunk where you want the child document to be inserted:
```{r child = 'my-child.Rmd'}
```

That’s it. When you render the parent document, the contents of the child will be included.

To prove it, here are the contents of the my-child.Rmd document:

I am from the child document even though my unformatted text doesn’t really show it.

2.1 This is a level 2 header

I love both of my children equally.

2.1.1 This is a level 3 header

As much as I love R Markdown, I love my children more.

3 Cautionary notes

The nice auto-numbering table of contents feature in the html_document and related output formats isn’t so smart. You’ll need to think about the header levels in your parent and child documents, and make sure that they work.

Your ‘child’ documents may or may not have information about variables you define in the parent document or other child documents.