Showing code chunk name in output in RMarkdown

前端 未结 1 1769
名媛妹妹
名媛妹妹 2021-01-19 03:58

As known in RMarkdown code chunks can be named like this:

```{r chunkname}

plot(x,y)

```

Is it possible to showing chunkname in output do

相关标签:
1条回答
  • 2021-01-19 04:32

    You can use knitr::opts_current$get()$label

    example:

    ```{r cars}
    library(knitr)
    opts_current$get()$label
    plot(cars)
    ```
    

    It will also work outside of a chunk, in an inline r code. It will then output the label of the last chunk.

    You can of course save the labels in a vector to use them later, for instance with a custom hook:

    ```{r knitr_setup}
    library(knitr)
    ll <- opts_current$get()$label
    knit_hooks$set(label_list = function(before, options, envir) {
        if(before) ll <<- c(ll,opts_current$get()$label)
    })
    opts_chunk$set(label_list=TRUE)
    ```
    

    ll will then contain the list of chunk labels. However, you cannot access the names of chunks not yet ran.

    0 讨论(0)
提交回复
热议问题