2016-04-20 35 views
0

在「fig_caption:真」的情況下在YAML頭,我試圖沿着r/rmarkdown/knitr:如何將評估r塊添加到圖形圖例?

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

然而線嵌入的代碼段的輸出在標題,結果是第r段/大塊正在打印逐字,而不是評估。我努力做到甚至現在可行嗎?

+0

沒有試過,但嘗試把整個標題成糊狀。像fig.caption ='r paste(「這是我的標題」,A,B)' –

回答

2

http://yihui.name/knitr/options/通過@Yihui:

eval.after:(fig.cap)選項名的字符向量;這些 選項將在評估塊後評估,並且所有其他 選項將在塊之前評估(例如,對於塊選項 fig.cap = paste('p-value is',t.test(x)$ p .value的),它會根據x的值來評估塊後 如果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
+0

謝謝你的答案和鏈接; – balin