Split r chunk header across lines in knitr

岁酱吖の 提交于 2020-01-05 03:39:50

问题


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?

E.g.:

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

回答1:


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

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.)



来源:https://stackoverflow.com/questions/33628318/split-r-chunk-header-across-lines-in-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!