knitr 将参数 fig.cap 定义为
fig.cap: (NULL; character) figure caption to be used in a figure environment in LaTeX (in \caption{}); if NULL or NA, it will be ignored, otherwise a figure environment will be used for the plots in the chunk (output in \begin{figure} and \end{figure})
但是,对于 HTML 输出,以下工作:
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
```
```{r, fig.cap = c("This is caption 1", "This is caption 2")}
## Plot 1
qplot(carat, price, data = diamonds)
## Plot 2
qplot(carat, depth, data = diamonds)
```
意思是,每个数字都有在代码块参数
fig.cap = c("Caption 1", "Caption 2")
中定义的正确标题然而,当它们被放置在 block 选项中时,跟踪字幕是具有挑战性的——尤其是在长时间的情况下。除了为每个图形创建两个单独的 block 并在 block 外插入标题之外,还有其他选择吗?
最佳答案
您可以设置eval.after="fig.cap"
以便在 block 运行后评估图形标题。这样,您可以在 block 内定义您的标题。
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
library(knitr)
opts_knit$set(eval.after = 'fig.cap')
```
```{r, fig.cap = cap}
## Plot 1
qplot(carat, price, data = diamonds)
cap <- "This is caption 1"
## Plot 2
qplot(carat, depth, data = diamonds)
cap <- c(cap, "This is caption 2")
```
关于r - HTML 中的 Knitr 和图形标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486935/