How to nest knit calls to fix duplicate chunk label errors?

前端 未结 2 1678
悲&欢浪女
悲&欢浪女 2021-01-13 18:02

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

相关标签:
2条回答
  • 2021-01-13 18:34

    I don't understand exactly the context use of your code. Why not to use simply knitr child document feature?

    Here a workaround ( hope that someone else come with a better solution specially you if you give more context)

    some_function <- function(chunk.name='chunk1'){
      knit(text =sprintf("
        ```{r %s}
            1
        ```
        ",chunk.name))
    }
    cat(knit(text ="
    ```{r }
        some_function('a1')   
    ```
    ```{r }
        some_function('a2')   
    ```
    "))
    
    0 讨论(0)
  • 2021-01-13 18:38

    You can use knit_child() instead of knit() in some_function():

    library(knitr)
    some_function <- function(){
      knit_child(text ="
        ```{r }
            1
        ```
        ")
    }
    cat(knit(text ="
    ```{r }
        some_function()   
    ```
    ```{r }
        some_function()   
    ```
    "))
    
    0 讨论(0)
提交回复
热议问题