knit()中的purl()重复标签错误

时间:2016-04-26 14:44:10

标签: r markdown rstudio knitr r-markdown

我正在编织一个.Rmd文件,并希望有两个输出:每次运行编织时,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()时都会得到html和.R输出(或点击RStudio中的knit)。

但是,如果您将块命名为失败。例如:

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输出。

2 个答案:

答案 0 :(得分:3)

您可以通过在文件中包含options(knitr.duplicate.label = 'allow')来允许重复标签,如下所示:

title: "Purl MWE"
output: html_document
---

```{r GlobalOptions}
options(knitr.duplicate.label = 'allow')
```


```{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
```

knitr网站上未记录此代码,但您可以直接从Github跟踪最新更改:https://github.com/yihui/knitr/blob/master/NEWS.md

答案 1 :(得分:0)

您可以使用在单独的R会话中调用purl的{​​{1}}块来避免此错误。这样就不需要允许重复标签了。

示例用例是一个Rmd文件,其中代码在整个报表中运行(而不是echo'),然后所有代码块都在附录中显示了块名称和代码注释。如果您不需要其他功能,则只需要bash块。

我们的想法是report_end表示停止purl的位置,以使附录代码不被视为“报告代码”。然后read_chunk将整个R文件读入一个代码块,然后可以echo进行语法高亮显示(如果需要)。

---
title: "Purl MWE"
output: html_document
---

These code chunks are used in the background of the report however
their source is not shown until the Appendix.

```{r test1, echo=FALSE}
x <- 1
x
```

```{r test2, echo=FALSE}
x <- x + 1
x
```

```{r test3, echo=FALSE}
x <- x + 1
x
```

# Appendix

```{r, eval=TRUE}
report_end <- "^# Appendix"
temp <- tempfile(fileext = ".R")
Sys.setenv(PURL_IN = shQuote("this_file.Rmd"), # eg. knitr::current_input()
           PURL_OUT = shQuote(temp),
           PURL_END = shQuote(report_end))
```


```{bash, include=FALSE}
Rscript -e "lines <- readLines($PURL_IN, warn = FALSE)" \
        -e "knitr::purl(text = lines[1:grep($PURL_END, lines)], output = $PURL_OUT, documentation = 1L)"
```

```{r, include=FALSE}
knitr::read_chunk(temp, labels = "appendix")
unlink(temp)
```

```{r appendix, eval=FALSE, echo=TRUE}
```