在Knitr中的行之间拆分R块头 [英] Split r chunk header across lines in knitr
问题描述
当我在R块标题中插入长标题之类的内容时,能够将标题分成多行会很好.
When I'm inserting long captions and the like in my R chunk header, it'd be nice to be able to split the header across multiple lines.
有没有简单的方法可以做到这一点?
Is there any easy way to do this?
例如:
```{r, echo=FALSE, warning=FALSE,
fig.cap="Here is my really long caption. It'd be nice to split this and other portions across lines"}
print(plot(x=runif(100),y=runif(100)))
```
推荐答案
否,您不能在块选项中插入换行符.从手册:
No, you cannot insert line breaks in chunk options. From the manual:
块选项必须写成一行;块选项中不允许使用换行符
Chunk options must be written in one line; no line breaks are allowed inside chunk options
但是,如果您极希望在编辑器中使用整洁的格式,则可以通过其他变量绕行,但这会使代码膨胀很多:
However, if you desperately want neat formatting in the editor you could take a detour via an additional variable, but this inflates the code quite a lot:
---
output:
pdf_document:
fig_caption: yes
---
```{r}
mycaption <- "This is my
very long caption
that spans over
several lines.
(in the editor)"
```
```{r, fig.cap = mycaption}
plot(1)
```
使用选项eval.after
,甚至可以在将其用作选项值的块中定义mycaption
:
With the option eval.after
it is even possible to define mycaption
within the chunk that uses it as option value:
---
output:
pdf_document:
fig_caption: yes
---
```{r}
library(knitr)
opts_knit$set(eval.after = "fig.cap")
```
```{r, fig.cap = mycaption}
mycaption <- "This is my
very long caption
that spans over
several lines.
(in the editor)"
plot(1)
```
(我假设问题是关于代码在编辑器中的外观,而不是关于输出中的换行符.)
(I assume that the question is about how the code looks (in the editor) not about a line break in the output.)
这篇关于在Knitr中的行之间拆分R块头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!