I'd like to run an R
script before building a bookdown book. The script runs purl()
on specific Rmd
Files which again are included in the book (an thus need an update since the Rmd Files might have changed).
I'd like to automatize this since it's easy to forget running the script before building the book. Also, I'd like to stick with the "build book" button since this is very convenient.
I tried including the script in the index.Rmd
File, but this lead to a "duplicate label" issue as described by this unanswered question. The error described by @Adam M. Wilson is persistent when using bookdown
even when completely omitting named chunks.
He're a minimal example using a minimal example of a bookdown project. The error is posted below.
Is there a workaround to this issue? Either (a) a solution for running purl()
within a chunk or (b) running an R-script automatically prior to building the book?
---
title: "A Book"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::gitbook: default
bookdown::pdf_book: default
---
# Hello World
```{r}
library(stringr)
rmds <- list.files(pattern = ".Rmd")
for (file in rmds){
file_r <- gsub("Rmd","R",file)
if(file.exists(file_r)){
file.remove(file_r)
}
knitr::purl(file,documentation = 0,output = file_r)
}
```
Hi.
Bye.
And here's the error message:
Quitting from lines 14-27 (index.Rmd)
Error in parse_block(g[-1], g[1], params.src) :
duplicate label 'unnamed-chunk-1'
Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block
Please delete _main.Rmd after you finish debugging the error.
Execution halted
Exited with status 1.