Stefano
Stefano

Reputation: 1485

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

I am trying to knit an .Rmd file (file2) with rmarkdown::render() from within another .Rmd file (file1).

If I run the chunk in file1 that renders file2 interactively from file1, file2 is rendered fine.

If I knit file1 I get this error:

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

How can I knit .Rmd files from other .Rmd files?

This is the MWE content of file1 and file2:

file1:

---
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)
```

Upvotes: 1

Views: 445

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30114

You should render the other file in another R session, e.g.,

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

Upvotes: 1

Related Questions