在knitr中打印打开的结束响应

时间:2014-11-25 19:08:23

标签: r knitr

我的调查数据包含一些问题,其中“其他”作为选项,受访者可以输入少量文字。我想使用knitr设置报告,其中打开的响应会自动打印。当我使用print函数时,每一行都显示如下:

## 1 This is an open ended response
## 2 Here is another response
## 3 I LOVE Stackoverflow, everyone is so helpful
## 4 knitr can be a real pain!

我尝试过使用gridTable,但似乎没有一个选项可以控制列数。有人能指出我正确的方向吗?

一些示例代码显示了我如何构建数据:

library(dplyr)

x <- c("Male", "Female", "Male", "Female", "Female", "Male", "Female")
y <- c(NA, NA, "This is an open ended response", NA, "Here is another response", "I LOVE Stackoverflow, everyone is so helpful", "knitr can be a real pain!")

z <- data.frame (x,y, stringsAsFactors = FALSE)

openResponse <- select (z, y) %>%
  filter (y != "NA")

print (openResponse)

我也曾在knitr中尝试过\Sexpr{openResponse},但它却是一个连续的字符串。我想把它分开来。

1 个答案:

答案 0 :(得分:0)

喜欢这个?这个针织大块:

```{r comment=NA, message=FALSE, warning=FALSE}
library(dplyr)
x <- c("Male", "Female", "Male", "Female", "Female", "Male", "Female")
y <- c(NA, NA, "This is an open ended response", NA, 
       "Here is another response", "I LOVE Stackoverflow, everyone is so helpful", 
       "knitr can be a real pain!")
z <- data.frame (x,y, stringsAsFactors = FALSE)
openResponse <- select (z, y) %>%
  filter (y != "NA")

print (openResponse)
```

产生这个:

由于加载message时的所有垃圾输出,我关闭了waringdplyr;您只需要comment=NA关闭##