根据 https://yihui.org/knitr/demo/child/,可以通过在块中使用set_parent((自行编织子文档。
我试过了:
knitr::set_parent("<PATH TO MAIN FILE>")
但这似乎行不通。编织子项不会考虑父项的 YAML 部分中的信息。我在这里做错了什么?
下面是一个函数,可用于将任意 Rmd 文件的 YAML 前言输入到另一个 Rmd 文件中:
input_yaml = function(file) {
lines = xfun::read_utf8(file)
meta = rmarkdown:::partition_yaml_front_matter(lines)$front_matter
knitr::asis_output(paste(meta, collapse = 'n'))
}
如果您不喜欢:::
,您还可以使用:
input_yaml = function(file) {
meta = rmarkdown::yaml_front_matter(file)
meta = c('---', yaml::as.yaml(meta), '---')
knitr::asis_output(paste(meta, collapse = 'n'))
}
然后在子文档中,您可以执行以下操作:
```{r, echo=FALSE}
input_yaml('parent.Rmd')
```
This is a child document without YAML.