簡體   English   中英

我可以在帶有eval = false的r markdown中產生圖形標題嗎?

[英]can I produce a figure caption in r markdown with eval=false?

如果我擁有MWE:

---

title: "Example"
output:
  pdf_document:
    fig_caption: yes

---


Text text text


```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
summary(cars) 
```

那么我就沒有標題。 但是,如果我這樣做:

---
title: "Example"
output:
  pdf_document:
    fig_caption: yes
---


Text text text


```{r fig.cap="Figure 1. Some random numbers"}
summary(cars) 
```

即刪除eval=FALSE則說明字幕不再加載。

為什么我要這樣做?

我想將示例代碼片段放入我的文檔中。 該代碼實際上將無法正常工作,因此為什么我要抑制它。 就像是

---

title: "Example"
output:
  pdf_document:
    fig_caption: yes
---


Text text text


```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
for (i in 1:length(c){
#do something
}
```

我只是在演示一個for循環,而沒有實際運行代碼。

據我所知,knitr默認情況下不支持代碼標題。 標記代碼塊的最簡單方法是在markdown框下方添加說明。

如果必須在r代碼中包含標題,則可以使用塊掛鈎 這是您的情況的示例:

---
title: "Example"
output:
  pdf_document:
    fig_caption: yes
---

```{r}
library(knitr)
knit_hooks$set(wrapper = function(before, options, envir) {
  if (!before) {
    sprintf(options$comment)
  }
})
```

```{r comment="Figure 1. Some random numbers",wrapper=TRUE,eval=FALSE}
for (i in 1:length(c){
#do something
}
```

我們定義了一個鈎子( wrapper ),如果我們在任何塊選項中調用wrapper=TRUE ,則comment參數將顯示在下面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM