Reputation: 69
I am trying to automate chunk creation in an R markdown document. I am using the iris dataset just for this question. With this dataset there are 3 chunks, but my goal is to be able to create a document with a variable number of chunks. What I have so far is in two files - Parent.Rmd and Child.Rmd. The contents of these two files is shown below. The 3 headers are giving me what I expect. The problem is with the print(a) statement in the Child.Rmd file. It only prints the same data.frame each time, the data.frame for the last species, virginica. How do I fix this? Am I on the right track or should I be tackling this problem in another way?
Your help is appreciated. Thank you.
Paul
Parent.Rmd
---
title: "Parent"
output: pdf_document
---
```{r setup, echo=FALSE}
library(knitr)
```
```{r include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species)))
{
a <- iris[iris$Species == i, ]
out = c(out, knit_expand(file = "Child.rmd"))
}
```
`r paste(knit(text = out), collapse = '\n')`
Child.Rmd
*********
#### Species = {{i}}
```{r echo=FALSE}
print(a)
```
Upvotes: 1
Views: 344
Reputation: 77116
You could perhaps do the subsetting in Child.Rmd
#### Species = {{i}}
```{r echo=FALSE}
a <- head(iris[iris$Species == "{{i}}", ])
print(a)
```
Upvotes: 2