gpt4 book ai didi

r - 在Knitr中的行之间拆分R block 头

转载 作者:行者123 更新时间:2023-12-04 12:02:56 27 4
gpt4 key购买 nike

当我在R块 header 中插入长标题等内容时,能够将 header 拆分为多行会很好。

有没有简单的方法可以做到这一点?

例如。:

```{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)))
```

最佳答案

不可以,您不能在块选项中插入换行符。从the manual:

Chunk options must be written in one line; no line breaks are allowed inside chunk options



但是,如果您非常希望在编辑器中使用整洁的格式,则可以通过其他变量绕行,但这会使代码膨胀很多:
---
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:
---
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)
```

(我假设问题是关于代码在编辑器中的外观,而不是输出中的换行符。)

关于r - 在Knitr中的行之间拆分R block 头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33628318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com