How to insert markdown in the middle of an knitr R code chunk?

有些话、适合烂在心里 提交于 2019-12-12 22:24:48

问题


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


来源:https://stackoverflow.com/questions/41133761/how-to-insert-markdown-in-the-middle-of-an-knitr-r-code-chunk

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