Use multiple instances of the same Rmd file as different pages on a bookdown site

I have a project that started out as a single .Rmd file. I published this single page by using rmarkdown::render_site() and moving the contents of the _site directly to my webserver.

I now need to make several pages which are almost identical except with some minor differences. I tried just making a copy of the index.Rmd file and giving it a new name. However, when I try to build the site I get an error message about duplicate chunk labels. Instead of just renaming each chunk in the new doc as e.g.

{r setup1 ...} # as opposed to just setup

Is there a right way to handle this?

Example script Copy1.Rmd:

---
title: "Home"
output: html_document
site: bookdown::bookdown_site
---


## R Markdown

{r setup}
library(odbc)
the_date <- '2020-04-30'
my_conn <- DBI::dbConnect(odbc(), "Athena")


{sql connection=my_conn, output.var="a_df"}
select *
from schema.table
where date <= ?the_date


{r cars}
str(a_df)

In _site.yml:

---
title: "Example"
navbar:
  title: "Example"
  left:
    - text: "Home"
    - href: "index.Rmd"
    - text: "Copy1"
    - href: "Copy1.Rmd"
    - text: "Copy2"
    - href: "Copy2.Rmd" 

If I want to make multiple copies of the example page above 'Copy1.Rmd' which each generate slightly different output based on the variable the_date within the chunk called setup, how ca I do that?

If I just make multiple copies and add each new copy as a page to _site.yml I get this error about duplicate chunk names. Once again I could just laboriously rename each chunk in a copied file e.g. setup1, setup2 etc but that seems 'wrong'.

What's the 'right' way to use multiple copies of the same file as different pages in my site?

rmarkdown::render_site(encoding = 'UTF-8')
processing file: _main.Rmd
Error in parse_block(g[-1], g[1], params.src) :
duplicate label 'get_data'
Calls: ... process_file -> split_file -> lapply -> FUN -> parse_block
Please delete _main.Rmd after you finish debugging the error.
Execution halted
Exited with status 1.

Just in case you don't know it, rmarkdown parametrized report allows you to use the same source code but render it differently based on parameters. It could be useful in your case.

About the issue you have, you can't compile a rmarkdown document (or several in the same session) if they have the same label name.

Can you try to add this in your _site.yml ?

new_session: yes

This should allow to compile each document in its own session independently. I think this could prevent the knitr chunk label constraint.

You can also look at other website generation tool like blogdown. Or you can also write you very own custom site generator

hope it helps

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.