防止行的最后一个单词在R Markdown报告中被分成几行

时间:2018-06-28 17:04:47

标签: r r-markdown

我想使用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”。如何使它“更好”地全部打印在一行上?

1 个答案:

答案 0 :(得分:0)

我喜欢使用cat()代替print(),并为块指定results='asis'

这样,文本直接通过html呈现。

###How can this teacher improve
```{r comment=NA, results='asis'}
cat(d$x)
```

html-preview-1

而不是通过html的<code/>块。

enter image description here

这里是第一张和第二张图片之间html的比较。请注意,第一种方法以<p>Test开头,而第二种方法是<pre><code>[1] &quot;

<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] &quot;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&quot;
[2] &quot;For this teacher to improve they will have make a better attempt to \nattend to the needs of all the students in the classroom&quot;                                                       </code></pre>
</div>