【问题标题】:purl() within knit() duplicate label errorknit()中的purl()重复标签错误
【发布时间】: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


【解决方案1】:

您可以通过在文件中包含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

【讨论】:

  • 虽然这是一个很好的解决方法,但这有点冒险。如果确实有重复的标签(除了 purl 问题)在生成的数字块中,您可能会丢失在不同地方生成的具有相同名称的一些图像。小心点。
【解决方案2】:

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

一个示例用例是一个 Rmd 文件,其中代码在整个报告中运行(而不是 echo'd),然后在附录中显示所有代码块以及块名称和代码 cmets。如果您不需要额外的功能,那么您只需要直到 bash 块。

这个想法是report_end 表示在哪里停止purl,这样附录代码就不会被视为“报告代码”。然后read_chunk 将整个 R 文件读入一个代码块,然后可以在需要时使用语法突出显示 echo'd。

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

【讨论】:

    猜你喜欢
    • 2016-07-01
    • 1970-01-01
    • 2020-06-30
    • 2017-01-19
    • 2013-06-22
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多