8

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)))
```
CL.
  • 14,577
  • 5
  • 46
  • 73

2 Answers2

19

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

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
CL.
  • 14,577
  • 5
  • 46
  • 73
  • 2
    That is right. It is by design that the chunk header must be one a single line. – Yihui Xie Nov 10 '15 at 17:53
  • @JEdwards Thanks for your interest in keeping answers on Stack Overflow up-to-date. However, edits should always respect the original intent of the answer author. Updates like these should [exist as separate answers](https://meta.stackoverflow.com/questions/400281/) as has already been posted. Instead, upvote the new, preferred solution so it rises in the answer order. – Ian Campbell Nov 02 '22 at 05:20
8

As of knitr v1.35 (https://github.com/yihui/knitr/releases/tag/v1.35) you are able to write chunk headers across multiple lines.

The syntax is slightly different from typical rmarkdown chunk syntax. To achieve your goal, you extend the rmarkdown chunk header into the comments of the chunk. You'd rewrite your example as follows:

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

Admittedly, that doesn't help handle having a very long figure caption, and the recommendation by CL to put the figure caption in a variable is still a good idea.

But, the new syntax also allows you to use yaml syntax to specify the chunk options, and yaml allows for multiline strings. So you could do the following:

```{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)))
```
Russ Hyde
  • 2,154
  • 12
  • 21
  • Actually, comment style chunk headers can be hard-wrapped across multiple lines without yaml. The feature (including an example with a long caption) is documented [here](https://yihui.org/knitr/options/#chunk-options) – fisher-j May 15 '22 at 16:47