0

I wrote an Rmd Markdown file main.Rmd which works on the basis of the data of a certain logfile (logfile1.Rda):

load("logfile1.Rda")

In this main.Rmd I refer to several child chunks:

{r child = 'child1.Rmd'}

and

{r child = 'child2.Rmd'}

and so on...

Now, I want to refer to a second logfile (logfile2.Rda):

load("logfile2.Rda")

and to reuse the child Rmd files again:

{r child = 'child1.Rmd'}

and

{r child = 'child2.Rmd'}

and so on...

But, as expected, there is the "duplicate chunk labels" warning.

Do you have an idea, how to reuse my child Rmd files which includes several chunks?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Does this help: `options(knitr.duplicate.label = "allow")` – PKumar Feb 26 '21 at 11:31
  • I already tried it, but nothing happened. **Where** do I have to add this? –  Feb 26 '21 at 11:32
  • Nothin happens ... –  Feb 26 '21 at 11:39
  • Not sure, It should have worked, read it here : https://bookdown.org/yihui/rmarkdown-cookbook/duplicate-label.html – PKumar Feb 26 '21 at 11:40
  • I cannot add it in my Rprofile, because I want others to reproduce my stuff. And adding it into the main.Rmd doesn't work –  Feb 26 '21 at 11:43
  • How about assigning numbers for chunks in your main.Rmd (i <- 1 for logfile 1 and after that i <- 2 for logfile 2) and then using them in the child as chunk name like ```{r chunk{{i}} ,eval = TRUE, results = "asis", echo=FALSE, warning=FALSE}` At least this worked for me when looping to create some plots with the same child.rmd You would need to use knit_expand in this case e.g. `knit_expand(text = readLines('child.rmd'), encoding = "UTF-8")` – kukuk1de Feb 26 '21 at 13:34
  • Works! See: https://stackoverflow.com/questions/12095113/r-knitr-possible-to-programmatically-modify-chunk-labels –  Mar 01 '21 at 20:33

1 Answers1

0

If found a solution:

load("logfile1.Rda")
z <- 1
src <- lapply(z, function(z) knitr::knit_expand(file = "child1.Rmd"))

r knitr::knit(text = unlist(src))

src <- lapply(z, function(z) knitr::knit_expand(file = "child2.Rmd"))

r knitr::knit(text = unlist(src))

load("logfile2.Rda")
z <- 1
src <- lapply(z, function(z) knitr::knit_expand(file = "child1.Rmd"))

r knitr::knit(text = unlist(src))

src <- lapply(z, function(z) knitr::knit_expand(file = "child2.Rmd"))

r knitr::knit(text = unlist(src))

Within the child-files I added {{z}} to each chunk label.