r/rmarkdown/knitr: 如何将评估 r 块添加到图形图例?

r/rmarkdown/knitr: How to add an evaluate r chunk to a figure legend?

在 yaml header 中 "fig_caption: true" 的上下文中,我试图将代码片段的输出嵌入标题中

```{r pressure, echo=FALSE, fig.caption="This is my caption `r paste('A','B')`;"}
plot(pressure)
```

然而,结果是 r snippet/chunk 被逐字打印 ,而不是被评估。我现在努力做的事情是否可行?

来自@Yihui 的http://yihui.name/knitr/options/:

eval.after: (fig.cap) a character vector of option names; these options will be evaluated after a chunk is evaluated, and all other options will be evaluated before a chunk (e.g. for chunk option fig.cap=paste('p-value is', t.test(x)$p.value), it will be evaluated after the chunk according to the value of x if eval.after='fig.cap')

简而言之,要使您的 paste 正常工作,请像这样使用 eval.after='fig.cap'

---
title: "Untitled"
author: "chinsoon12"
date: "April 21, 2016"
output: 
  html_document:
    fig_caption: true
---


```{r, echo=FALSE}
A <- "A"
B <- "B"
```


```{r pressure, echo=FALSE, eval.after='fig.cap', fig.cap=paste("This is my caption", A)}
A <- "A"
B <- "B"
plot(cars)
```

您可能还想查看这些:

  1. Inserting Captions and Cross References in a R Markdown Document by Andy Lyons
  2. Figure and Table Captions in Markdown by fishR Blog