在 RMarkdown 的输出中显示代码块名称 [英] Showing code chunk name in output in RMarkdown

查看:91
本文介绍了在 RMarkdown 的输出中显示代码块名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,RMarkdown 代码块可以这样命名:

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?

推荐答案

您可以使用 knitr::opts_current$get()$label

示例:

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

它也可以在块之外的内联 r 代码中工作.然后它将输出最后一个块的标签.

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 然后将包含块标签列表.但是,您无法访问尚未运行的块的名称.

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

这篇关于在 RMarkdown 的输出中显示代码块名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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