Showing code chunk name in output in RMarkdown

痞子三分冷 提交于 2020-01-11 09:51:33

问题


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

```{r chunkname}

plot(x,y)

```

Is it possible to showing chunkname in output document?


回答1:


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.



来源:https://stackoverflow.com/questions/35038607/showing-code-chunk-name-in-output-in-rmarkdown

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