【发布时间】:2017-11-01 22:34:23
【问题描述】:
我想在构建一本书籍之前运行一个R 脚本。该脚本在特定的Rmd 文件上运行purl(),这些文件再次包含在书中(因此需要更新,因为 Rmd 文件可能已更改)。
我想自动执行此操作,因为在构建本书之前很容易忘记运行脚本。另外,我想坚持使用“构建书”按钮,因为这非常方便。
我尝试将脚本包含在index.Rmd 文件中,但这会导致this unanswered question 所述的“重复标签”问题。 @Adam M. Wilson 描述的错误在使用 bookdown 时仍然存在,即使完全省略了命名块。
他是使用a minimal example of a bookdown project 的最小示例。错误发布在下面。
这个问题有解决方法吗? (a) 在块内运行 purl() 的解决方案或 (b) 在构建书籍之前自动运行 R 脚本?
---
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.
这是错误消息:
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.
【问题讨论】:
-
我已经回答了你提到的另一个问题。让我知道是否有帮助:stackoverflow.com/questions/36868287/…
-
允许重复标签解决了这个问题!非常感谢:-)
标签: r knitr r-markdown bookdown