0

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.
Ratnanil
  • 1,641
  • 17
  • 43
  • 1
    I have answered the other question you referred to. Let me know if that helps: https://stackoverflow.com/questions/36868287/purl-within-knit-duplicate-label-error – Michael Harper Nov 01 '17 at 23:57
  • allowing duplicate labels solved the issue! Thank you very much :-) – Ratnanil Nov 02 '17 at 10:47

1 Answers1

0

You use same name in the markdown file. For example

{r relationship between gender and health} .... ....

{r relationship between gender and health} .... ....

Then you will meet an error when knit

  • I'm not really sure if you've understood the question..I've not even specified names for my chunks. As seen in the error message, the label that is apparently duplicate is `unnamed-chunk-1`. Anyway, the question has already been answered in the [linked question](https://stackoverflow.com/q/36868287/4139249) – Ratnanil Oct 15 '18 at 12:26