2016-02-18 38 views
2

knitr定義參數fig.cap作爲Knitr和圖標題在HTML

fig.cap:(NULL;字符)被在一個數字 環境中乳膠使用圖題(在\字幕{});如果NULL或NA,它將被 忽略,否則一個數字環境將在 用於重複塊(輸出在\開始{數字}和\端{圖})

然而,對於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")

但是其正確的標題,它是具有挑戰性的跟蹤字幕的 - 特別是如果他們長期放置時在春天裏面k選項。除了爲每個圖形創建兩個單獨的塊並在字塊外插入字幕之外,還有其他選項嗎?

回答

6

您可以設置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") 
``` 
+3

這將是knitr的下一個版本的默認:https://github.com/yihui/knitr/commit/e3e3e1b8e3640707e03af2b521159abb8b214d70 –