【问题标题】:R Bookdown run script / purl before building book (with Build Book button)R Bookdown 在构建书籍之前运行脚本/ purl(带有构建书籍按钮)
【发布时间】: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.

【问题讨论】:

标签: r knitr r-markdown bookdown


【解决方案1】:

您在降价文件中使用相同的名称。例如

{r 性别与健康的关系} …… ....

{r 性别与健康的关系} …… ....

那么你在编织的时候会遇到一个错误

【讨论】:

  • 我不确定你是否理解了这个问题。我什至没有为我的块指定名称。如错误消息中所示,显然重复的标签是unnamed-chunk-1。无论如何,问题已经在linked question
猜你喜欢
  • 2021-04-30
  • 2021-09-30
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多