在knit()中的purl()重复标签错误 [英] purl() within knit() duplicate label error

查看:85
本文介绍了在knit()中的purl()重复标签错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编织.Rmd文件,希望每次运行knit时都有两个输出:html和Purl'ed R脚本.可以使用以下Rmd文件完成此操作:

I am knitting a .Rmd file and want to have two outputs: the html and a purl'ed R script each time I run knit. This can be done with the following Rmd file:

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

If you do not name the chunk, it works fine and you get html and .R output each time you run knit() (or click knit in RStudio).

但是,如果您命名该块,它将失败.例如:

However, if you name the chunk it fails. For example:

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()认为即使没有重复,也存在重复的块名称.

If you comment out the purl() call, it will work with the named chunk. So there is something about how the purl() call is also naming chunks which causes knit() to think there are duplicate chunk names even when there are no duplicates.

是否可以在.Rmd文件中包含purl()命令,以便生成两个输出(html和R)?还是有更好的方法来做到这一点?我的最终目标是使用新的rmarkdown::render_site()建立一个网站,该网站每次编译时都会更新HTML和R输出.

Is there a way to include a purl() command inside a .Rmd file so both outputs (html and R) are produced? Or is there a better way to do this? My ultimate goal is to use the new rmarkdown::render_site() to build a website that updates the HTML and R output each time the site is compiled.

推荐答案

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

You can allow duplicate labels by including options(knitr.duplicate.label = 'allow') within the file as follows:

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

This code isn't documented on the knitr website, but you can keep track with the latest changes direct from Github: https://github.com/yihui/knitr/blob/master/NEWS.md

这篇关于在knit()中的purl()重复标签错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆