1

I am using rmarkdown to do 5 main steps: reading, cleaning, transformations, analysis and viz.

My plan is to divide the code for each step into one or two markdown files (.Rmd). Each following .Rmd will be called as an external file into the next .Rmd (i.e I want to nest my .Rmd of each step into the next one).

I am nesting the .Rmd files by replacing:

knitr::opts_chunk$set(echo = TRUE)

with:

knitr::knit("**ABSOLUTE PATH TO PREVIOUS .Rmd FILE**")

This worked, until my 3rd nesting, when I get an error at the knitr::knit line:

Error in file(file, "rt" ) Cannot open the connection calls: <Anonymous> … withVisible ->eval ->eval -> read.csv ->read.table -> file

NOTE: every time I have referenced anything this has been done with absolute paths, so this shouldn't be the issue.

Can anyone point me to the correct way of nesting .Rmd into eachother?

Finally, if this workflow seems savage, I would welcome any other suggestions on architecture! My reasons for the nesting (as opposed to putting everything in a big R Notebook are) are:

  1. I want to work in .Rmd 'pages' to have separate tabs with less code I need to scroll through (and potentially mess up by accident).
  2. I can pass data from one step to the other without losing time to write it somewhere on my disk. (Basically I want to be able to evaluate and use the results from the previous .Rmd)
  3. I can make a clean html or pdf page for every step of the process.
BonnieB
  • 129
  • 2
  • 12

1 Answers1

1

To combine multiple child Rmd files, I use a general Rmd one that call all childs. In that way, you can also decide not to show some of them with eval=FALSE directly in this main file.
More info on https://yihui.name/knitr/demo/child/

The main file would look like any Rmd:

---
title: Modelling distribution of species in the Bay of Biscay
author: "StatnMap - Sébastien Rochette"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

<!-- Content -->

```{r reading, child='Reading.Rmd', eval=TRUE}
```

```{r cleaning, child='Cleaning.Rmd', eval=TRUE}
```

```{r analysis, child='Analysis.Rmd', eval=TRUE}
```
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43