如何在rmarkdown中显示结果为'asis'的格式化R输出 [英] How to show formatted R output with results='asis' in rmarkdown

查看:38
本文介绍了如何在rmarkdown中显示结果为'asis'的格式化R输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用result ='asis'时,有没有办法在rmarkdown/knitr中显示格式器R的输出?

Is there a way of showing formatter R output in rmarkdown/knitr when using results = 'asis'?

一个示例是以下功能:

myfun <- function() {
  cat("hello!\n")
  cat(c("one" = 1, "two" = 2))
}

然后,该块将在新行上打印第二个 cat :

Then, this chunk will print the second cat on a new row:

```{r}
myfun()
```

但这将忽略 myfun 的格式:

```{r, results = "asis"}
myfun()
```

是否可以保持 results ='asis',但同时保持 myfun 的输出符合预期的格式?

Is there a way of keeping results='asis' but at the same time keep the output of myfun formatted as intended?

推荐答案

如果您愿意在末尾添加两个或多个空格,则可以使用knitr块选项 results ="asis" 线.也就是说,您需要编写"hello \ n" 来触发换行,而不是"hello \ n" .

You can use the knitr chunk option results = "asis" if you are happy to add two or more spaces at the end of the line. That is, instead of "hello\n", you need to write "hello \n" to trigger the line break.

R Markdown代码示例:

Example R Markdown code:

---
output: html_document
---

```{r}
myfun <- function() {
  cat("hello!  \n")
  cat(c("one" = 1, "two" = 2))
}
```

```{r results = "asis"}
myfun()
```

给予

为什么空白?这是因为在一行的末尾使用两个空格来表示Markdown中的强行中断.例如,此报价来自 Pandoc的Markdown (这是默认的Markdown风味R Markdown用途):

Why the blank spaces? It's because two spaces at the end of a line are used to indicate a hard line break in markdown. For example, this quote is taken from Pandoc's Markdown (which is the default markdown flavour R Markdown uses):

段落
段落是一行或多行文本,后接一行或多行空白行.换行符被视为空格,因此您可以根据需要对段落进行重排.如果需要强行换行,请在行尾放置两个或多个空格.

Paragraphs
A paragraph is one or more lines of text followed by one or more blank lines. Newlines are treated as spaces, so you can reflow your paragraphs as you like. If you need a hard line break, put two or more spaces at the end of a line.

这篇关于如何在rmarkdown中显示结果为'asis'的格式化R输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆