如何嵌套编织调用以修复重复的块标签错误? [英] How to nest knit calls to fix duplicate chunk label errors?

查看:181
本文介绍了如何嵌套编织调用以修复重复的块标签错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在knit调用中调用使用knit的函数时,遇到了重复标签错误.如果我标记了大块,问题就解决了.有没有一种方法可以以与父knit调用不冲突的方式调用some_function?

I'm running into a duplicate label error when I call a function that uses knit inside a knit call. If I label the chunks the problem goes away. Is there a way to call some_function in a way that doesn't collide with the parent knit call?

library(knitr)
some_function <- function(){
    knit(text ="
    ```{r }
        1
    ```
    ")
}
cat(knit(text ="
```{r }
    some_function()   
```
```{r }
    some_function()   
```
"))

输出:

```r
some_function()
```

```
## Error: duplicate label 'unnamed-chunk-1'
```

推荐答案

您可以使用knit_child()代替some_function()中的knit():

library(knitr)
some_function <- function(){
  knit_child(text ="
    ```{r }
        1
    ```
    ")
}
cat(knit(text ="
```{r }
    some_function()   
```
```{r }
    some_function()   
```
"))

这篇关于如何嵌套编织调用以修复重复的块标签错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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