With the intention of making a project more manageable, I am trying to split up a long Rmd file into several smaller R scripts, each source
d by a master Rmd file.
The problem I am having is that plots generated by the subsidiary R scripts are not being included in the knitted output from the Rmd file.
I have made a small, hopefully reproducible, example to demonstrate the problem.
A. This works when knitted with knitr
:
---
title: "Test"
output:
html_document:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
error = TRUE,
message = FALSE,
warning = FALSE,
cache = FALSE,
out.width = "80%")
library(ggplot2)
```
```{r test_chunk}
ggplot(trees, aes(Girth, Height)) +
geom_point()
```
B. This doesn't:
---
title: "Test"
output:
html_document:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
error = TRUE,
message = FALSE,
warning = FALSE,
cache = FALSE,
out.width = "80%")
library(ggplot2)
```
```{r test_chunk}
source("01_plot.R")
```
where 01_plot.R
is:
ggplot(trees, aes(Girth, Height)) +
geom_point()
i.e. the same code as included in the chunk in example A.
Now I can't find too much online about to use source
in an RMarkdown document, but this post from Yihui implies that it should be the way to read in and evaluate external code. So what's wrong here?