knitr:检索ar chunk中的数字标题

Tin*_*ero 5 r knitr r-markdown

在我的rmarkdown文件中,我想知道是否可以在ar块内部并使用r-chunk本身内的fig.cap选项值.

例如:

```{r fig.cap = 'test'}
code
.
.
print(options$fig.cap)?
````
Run Code Online (Sandbox Code Playgroud)

在此先感谢任何帮助或建议,从哪里开始寻找

raw*_*awr 1

有趣的问题。我想知道执行此操作的正确方法,但这种(非常)hacky 方法对我有用。

---
output: 
  html_document:
    css: ~/knitr.css
---

```{r, include=FALSE}
library(knitr)
knit_hooks$set(plot = function(x, options) {
  fig_fn = paste0(opts_knit$get('base.url'), paste(x, collapse = '.'))
  fig.cap <<- knitr:::.img.cap(options)
  sprintf("<figure><img src='%s'><figcaption>%s</figcaption></figure>",
          fig_fn, fig.cap)
  })
```

```{r, fig.cap = 'Figure I: the plot of my figure.'}
plot(1:5)
````

I say some things and some other things.

Oh, yeah please refer to `r fig.cap`
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这适用于生成的最新图形,但您可以在图形计数器或其他东西中工作,为每个标题创建唯一的变量,以便您可以随时引用。