How to insert markdown in the middle of an knitr R

2019-07-21 06:23发布

问题:

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?

回答1:

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


标签: r markdown knitr