Knitr: Get the picture title inside fragment r

In my rmarkdown file, I was wondering if it is possible to be inside the r-fragment and use the value of the fig.cap parameter inside r-chunk itself.

For instance:

```{r fig.cap = 'test'} code . . print(options$fig.cap)? ```` 

Thanks in advance for your help or advice on where to start looking.

+5
source share
2 answers

Interest Ask. I would like to know the right way to do this, but this (very) hacker way works for me.

 --- 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` 

enter image description here

This works for the most recently created shape, but you can work on the shape counter or something else to create unique variables for each label so you can refer whenever you want.

+1
source

It can be found using knitr::opts_current$get("fig.cap") . Here is an example:

 ```{r fig.cap = 'test'} library(knitr) code . . print(opts_current$get("fig.cap")) ```` 
+1
source

Source: https://habr.com/ru/post/1211666/


All Articles