问题
在R Markdown (.Rmd)文档中的代码块中,如何解析包含换行符\n
的字符串,以便将文本显示在新行上?
数据和示例
我想要解析text <- "this is\nsome\ntext"
,并显示为:
this is
some
text
以下是一个示例代码块,其中包含几个尝试(但未能产生所需的输出):
```{r, echo=FALSE, results='asis'}
text <- "this is\nsome\ntext" # This is the text I would like displayed
cat(text, sep="\n") # combines all to one line
print(text) # ignores everything after the first \n
text # same as print
```
附加信息
该文本将来自Shiny应用程序中的用户输入。
例如,ui.R
tags$textarea(name="txt_comment") ## comment box for user input
接下来我有一个下载
按钮,它使用一个.Rmd
文档来渲染输入:
```{r, echo=FALSE, results='asis'}
input$txt_comment
```
这里有一个例子,在R Studio画廊中,点击这里查看
> cat("hello\nworld\n")
hello
world
但不知道在Rmarkdown中的结果如何。 - Sigve Karolius\n
,但它会在一行输出。我想要的是在\n
处换行。 - tospigknit
函数用于创建一个markdown文件,然后由pandoc或其他一些markdown解析器解析以生成最终文档(?在此思考)。你有尝试查看markdown文件吗?我的观点是,如果输出(来自cat
)在常规markdown文本字段中打印,则需要换行和两个空格才能被解析为markdown的“新行”。 - Sigve Karolius