我想使用R Markdown来打印对调查问题的开放式答案(任何长度的字符串),并且我希望所有答案都在同一标题下。例如,问题“这位老师如何改进?”存储在名为d
的变量中的名为x
的数据帧中。
这里是一个例子:
---
title: "Teacher Survey"
output:
html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r}
d <- data.frame(id = (1:2), x =c("Test to see if word gets split: I kind
of think that for this teacher to improve they will have make a better
attempt to attend to the needs of all the students in the classroom",
"For this teacher to improve they will have make a better attempt to
attend to the needs of all the students in the classroom"),
stringsAsFactors = FALSE)
```
###How can this teacher improve
```{r comment=NA}
print(d$x)
```
第一个响应在第一行上打印“ bette”,在第二行上打印“ r”,而不是在一行上都打印“ better”。如何使它“更好”地全部打印在一行上?
答案 0 :(得分:0)
我喜欢使用cat()
代替print()
,并为块指定results='asis'
。
这样,文本直接通过html呈现。
###How can this teacher improve
```{r comment=NA, results='asis'}
cat(d$x)
```
而不是通过html的<code/>
块。
这里是第一张和第二张图片之间html的比较。请注意,第一种方法以<p>Test
开头,而第二种方法是<pre><code>[1] "
。
<div id="how-can-this-teacher-improve" class="section level3">
<h3>How can this teacher improve</h3>
<p>Test to see if word gets split: I kind of think that for this teacher to improve they will have make a better attempt to attend to the needs of all the students in the classroom For this teacher to improve they will have make a better attempt to attend to the needs of all the students in the classroom</p>
</div>
<div id="how-can-this-teacher-improve" class="section level3">
<h3>How can this teacher improve</h3>
<pre><code>[1] "Test to see if word gets split: I kind \nof think that for this teacher to improve they will have make a better \nattempt to attend to the needs of all the students in the classroom"
[2] "For this teacher to improve they will have make a better attempt to \nattend to the needs of all the students in the classroom" </code></pre>
</div>