R Markdown-自动块? [英] R Markdown - Automated Chunks?
问题描述
我正在尝试在R markdown文档中自动创建块.我仅将虹膜数据集用于此问题.有了这个数据集,有3个块,但是我的目标是能够创建具有可变数量的块的文档.到目前为止,我有两个文件-Parent.Rmd和Child.Rmd.这两个文件的内容如下所示.这3个标题会给我我所期望的.问题出在Child.Rmd文件中的print(a)语句.每次只打印相同的data.frame,最后一个物种virginica的data.frame.我该如何解决?我是在正确的道路上还是应该以其他方式解决这个问题?
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.
保罗
Parent.Rmd
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)
```
推荐答案
您也许可以在Child.Rmd
#### Species = {{i}}
```{r echo=FALSE}
a <- head(iris[iris$Species == "{{i}}", ])
print(a)
```
这篇关于R Markdown-自动块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!