如何在针织R代码块的中间插入markdown? [英] How to insert markdown in the middle of an knitr R code chunk?
问题描述
例如,我想在下面的代码块中的两个图之间插入一个中断,而不将其拆分:
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)
```
结果如下:
```{r}
plot(1:100, 1:100)
````
<br>
```{r}
plot(1:100, 1:100)
```
如果results='asis'
是一个大块选项,则看起来像您可以直接打印<br>
命令,例如:
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?
推荐答案
您可以使用knitr
中的函数asis_output()
仅按原样输出<br>
.因此,例如,您可以执行以下操作:
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)
```
这比对整个块使用results = 'asis'
选项要好,因为这两个图不受影响.
This is better than using the results = 'asis'
option for the whole chunk because the two plots are not affected.
请注意,如果您要编织成pdf,这也适用于乳胶,但是必须避免反斜线的出现.例如:
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!