Showing code chunk name in output in RMarkdown

杀马特。学长 韩版系。学妹 提交于 2019-12-02 03:43:19

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!