I have to make a report which consists of parts that are always the same just with different filters. So I made one Rmd-file and loop over them. Now I have the problem that I want to cross reference a section. In the report it references to the first appearance of this section and not the one corresponding to the part. For example: All references are to section 1, but they should be the following: the first reference should be to section 1, the second reference to section 2, the third reference to sections 3 and so on. Is there a way to create unique identifiers via a string or add a unique part corresponding to the specific part? And if so, how do I reference them? Here is a short example:
index.Rmd code for report parts
# Some header {#headerID}
Some text.... `r name[i]`
Some more text... \@ref(headerID)
R code to create report
library(tidyverse)
library(officer)
library(bookdown)
library(officedown)
name <- c("name 1", "name 2", "name 3")
default_root <- getwd()
# create parts of reports
for (i in 1:length(name)){
bookdown::render_book("index.Rmd", output_format = officedown::rdocx_document(reference_docx = 'word_template.docx')
file.copy("_main.docx", default_root)
file.rename(paste0(default_root, "/_main.docx"), paste0(default_root, "/", name[i], ".docx"))
}
# combine parts of the report into one
part_reports<- list.files(default_root)
report <- read_docx(paste0(default_root, part_reports[1))
for (i in 2:length(part_reports)){
report <- body_add_docx(report , src = paste0(default_root, "/", part_reports[i]))
}
print(report, target = "Report.docx")
I tried to just pass a string like paste0(headerID, name[i])
instead of just headerID
but it does not render correctly and just prints this part instead of using it as identifier.