[英]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.