R - 编织一个 Rmd 文件时出错,该文件具有编织另一个 Rmd 文件的代码(重复 "setup" 块名称)

R - Error when knitting an Rmd file which has code to knit another Rmd file (duplicate "setup" chunk name)

我正在尝试从另一个 .Rmd 文件(file1).

如果我 运行 file1 中的块从 file1[=44] 交互呈现 file2 =], file2 渲染正常。

如果我编织 file1 我得到这个错误:

Error in parse_block(g[-1], g[1], params.src, markdown_mode) : 
  Duplicate chunk label 'setup', which has been used for the chunk:
...

如何从其他 .Rmd 个文件编织 .Rmd 个文件?

这是file1file2的MWE内容:

文件1:

---
title: "File 1"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = here::here())
```

# Let's knit it

```{r knit-it}
rmarkdown::render("./file2.Rmd", envir = new.env())
```

file2:

---
title: "File 2"
output: github_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = here::here())
```

# Knit me

```{r random}
rnorm(2)
```

您应该在另一个 R 会话中渲染另一个文件,例如,

xfun::Rscript_call(rmarkdown::render, list("./file2.Rmd"))