12.4 Allow duplicate labels in code chunks
By default, knitr does not allow duplicate code chunk labels in the document. Duplicate labels will result in an error when the document is knitted. This occurs most frequently when a code chunk is copied and pasted within a document. You may have seen an error message like this:
processing file: myfile.Rmd Error in parse_block(g[-1], g, params.src) : duplicate label 'cars' Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block Execution halted
However, there are scenarios where we may wish to allow duplicate labels. For example, if we have one parent document
parent.Rmd in which we knit the child document multiple times, it will fail:
In this scenario, we can allow duplicate labels by setting this global option in R before the child document is knitted:
You should set this option with caution. As with most error messages, they are there for a reason. Allowing duplicate chunks can create silent problems with figures and cross-references. For example, if two code chunks have the same label and both chunks generate plots, their plot files will overwrite each other (without an error or warning message), because the filenames of plots are determined by the chunk labels.