【发布时间】:2016-08-20 11:31:12
【问题描述】:
我正在编织一个 .Rmd 文件,并且希望在每次运行 knit 时有两个输出:html 和 purl'ed R 脚本。这可以通过以下 Rmd 文件来完成:
---
title: "Purl MWE"
output: html_document
---
```{r}
## This chunk automatically generates a text .R version of this script when running within knitr.
input = knitr::current_input() # filename of input document
output = paste(tools::file_path_sans_ext(input), 'R', sep = '.')
knitr::purl(input,output,documentation=1,quiet=T)
```
```{r}
x=1
x
```
如果您不命名该块,它可以正常工作,并且每次运行 knit()(或单击 RStudio 中的 knit)时都会得到 html 和 .R 输出。
但是,如果您命名该块,它会失败。例如:
title: "Purl MWE"
output: html_document
---
```{r}
## This chunk automatically generates a text .R version of this script when running within knitr.
input = knitr::current_input() # filename of input document
output = paste(tools::file_path_sans_ext(input), 'R', sep = '.')
knitr::purl(input,output,documentation=1,quiet=T)
```
```{r test}
x=1
x
```
它失败了:
Quitting from lines 7-14 (Purl.Rmd)
Error in parse_block(g[-1], g[1], params.src) : duplicate label 'test'
Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted
如果您注释掉 purl() 调用,它将与命名的块一起使用。因此,purl() 调用也命名了块,这导致 knit() 认为即使没有重复块名称也存在重复块名称。
有没有办法在 .Rmd 文件中包含 purl() 命令,以便生成两个输出(html 和 R)?还是有更好的方法来做到这一点?我的最终目标是使用新的rmarkdown::render_site() 构建一个网站,每次编译网站时都会更新 HTML 和 R 输出。
【问题讨论】:
-
你有没有想过这个问题?
-
我也有这个问题......非常令人沮丧。我想给我的块命名,但因为这个我现在不能。
标签: r markdown rstudio knitr r-markdown