2

For example, I would like to insert a break between each of the two plots in the following code chunk without breaking it up:

```{r}
plot(1:100, 1:100)
plot(1:100, 1:100)
```

such that the result is like:

```{r}
plot(1:100, 1:100)
````

<br>

```{r}
plot(1:100, 1:100)
```

If results='asis' is a chunk option, it looks like you can directly print the <br> command, e.g.:

```{r}
plot(1:100, 1:100)
print('<br>')
plot(1:100, 1:100)
```

What do I do for other types of chunks?

Community
  • 1
  • 1
Alex
  • 15,186
  • 15
  • 73
  • 127
  • 1
    Is this something that can be handled by using `par(mfrow=c(2,1))`? – r2evans Dec 14 '16 at 02:24
  • `par` might be able to insert a break, but I assume not arbitrary markdown code like section references. Also, it is much easier if you can use `
    ` in this case and not have to worry about how many plots you are going to create.
    – Alex Dec 14 '16 at 02:55
  • 1
    I think you'll need your code chunk to plot just one image. Perhaps `knitr::knit_child` is more appropriate? – r2evans Dec 14 '16 at 03:05
  • try replacing `print('
    ')` with `asis_output('
    ')` but without the `results = 'asis'` option.
    – acylam Dec 14 '16 at 04:07
  • Thanks @useR, can you please post that as an answer. That helped me to insert table captions as well using the `captioner` package -> `asis_output(table_nums("Table 1"))`! – Alex Dec 14 '16 at 04:16

1 Answers1

5

You can use the function asis_output() in knitr to only output <br> as is. So for instance, you can do this:

```{r}
plot(1:100, 1:100)
asis_output('<br>')
plot(1:100, 1:100)
```

This is better than using the results = 'asis' option for the whole chunk because the two plots are not affected.

Note that this will also work for latex if you are knitting to pdf, but back slashes will have to be escaped. For example:

```{r}
plot(1:100, 1:100)
asis_output("\\\\newline")
plot(1:100, 1:100)
```
acylam
  • 18,231
  • 5
  • 36
  • 45
  • Note: `asis_output()` is not supported in loops/iterations - https://www.rdocumentation.org/packages/knitr/versions/1.26/topics/asis_output – naaman Jan 07 '20 at 21:15