Resolving Duplicate Labels Error Code Chunk Externalization and Rmarkdown Notebook

Hi, I have the similar issue. Error creating notebook: duplicate label '...', when I try to use knitr’s code externalization, namely, knitr::read_chunk() in the notebook’s setup chunk.

I copied exactly what Yihui showed in his book: R Markdown: The Definitive Guide

example.Rmd

```{r setup}

knitr::read_chunk("example.R")

```

example.R

## ---- chunk
1 + 1

When I restart R and run all Chunks, it is good. However, when I try to save the notebook, the error pop up. I don't think I have any duplicate labels. Following is the image of my result.

1 Like

knitr require unique label for chunk. Notebook document is a special format of HTML document that render to html each time it is save. As you cannot render with duplicated label, it cannot be save. For html_document format, there is no issue with saving but you will have an error when rendered.

1 Like

I understand what you say. There should be no duplicated chunk labels.

However, I don't think my chunk labels are duplicated. If you think my chunk labels are duplicated, could you please point out how they are duplicated?

I just wanted to knitr a R script following Yihui's book, but I got an error.

If you don't put the line break between the first three and last three back-ticks this error doesn't occur.

You have

 ```{r chunk}

 ```

You need

 ```{r chunk}  
 ```
5 Likes

@aosmith was faster than me to answer! thank you !