问题
在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
```
附加信息
文字将来自闪亮应用程序上的用户输入。
例如ui.R
tags$textarea(name="txt_comment") ## comment box for user input
然后download
,我有一个使用.Rmd
文档来呈现输入的按钮:
```{r, echo=FALSE, results='asis'}
input$txt_comment
```
R Studio画廊中的示例
诀窍是"\n"
连续使用两个空格:因此替换"\n"
为" \n"
例子:
```{r, results='asis'}
text <- "this is\nsome\ntext"
mycat <- function(text){
cat(gsub(pattern = "\n", replacement = " \n", x = text))
}
mycat(text)
```
结果:
PS:这在SO(正常减价行为)上是相同的
如果您只想换行,请在行尾使用两个空格
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句