knitr 将参数 fig.cap 定义为
但是,对于 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")
中定义的正确标题然而,当标题被放置在块选项中时,跟踪标题是具有挑战性的 - 特别是如果很长的话。除了为每个图形创建两个单独的块并在块外插入标题外,还有其他选择吗?
最佳答案
您可以设置 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")
```
关于r - HTML 中的针织和图形标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35486935/