HTML中的Knitr和Figure Caption

Tho*_*del 3 r ggplot2 knitr r-markdown

knitr将参数fig.cap定义为

fig.cap :( NULL; character)在LaTeX的数字环境中使用的数字标题(在\ caption {}中); 如果是NULL或NA,它将被忽略,否则图形环境将用于块中的图(在\ begin {figure}和\ 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)
```
Run Code Online (Sandbox Code Playgroud)

意思是,每个数字都会在代码块参数中定义正确的标题 fig.cap = c("Caption 1", "Caption 2")

但是,跟踪字幕是很有挑战性的 - 特别是如果长时间将它们放在块选项中.除了为每个图形创建两个单独的块,并且在块外部插入了标题,还有其他选项吗?

sco*_*coa 9

您可以进行设置,eval.after="fig.cap"以便运行块评估图形标题.这样,您就可以在块内定义标题.

---
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")
```
Run Code Online (Sandbox Code Playgroud)

  • 这将是下一版knitr的默认值:https://github.com/yihui/knitr/commit/e3e3e1b8e3640707e03af2b521159abb8b214d70 (4认同)