knitr:如何防止输出中的文本换行?

sus*_*joh 43 r knitr

编织到HTML时,我遇到了knitr中代码输出块中的文本换行问题.

例如,如果我运行以下内容:

matrix(rnorm(60, 5, 2), ncol = 12)
Run Code Online (Sandbox Code Playgroud)

HTML中的输出将包装表,给出这样的输出,其中第12列在其余部分下移动:

##       [,1]   [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11]
## [1,] 3.407 0.8035 2.981 5.269 6.989 5.107 7.143 3.127 3.624 7.220 4.805
## [2,] 3.907 5.5971 5.488 4.995 6.496 5.980 1.576 3.009 6.605 3.440 2.754
## [3,] 1.945 3.7668 4.860 2.945 3.663 5.945 7.168 2.012 5.873 8.190 7.441
## [4,] 4.893 6.2054 4.403 3.967 2.880 7.196 1.813 3.283 5.216 5.699 2.829
## [5,] 5.706 0.9084 5.802 1.404 3.122 1.866 6.613 3.299 4.990 3.645 3.766
##       [,12]
## [1,] 0.3951
## [2,] 4.0866
## [3,] 5.9293
## [4,] 6.4729
## [5,] 2.7172
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以调整输出块的宽度,这样我就可以有一个表,其中行显示在一行上,就像这样?

##       [,1]   [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
## [1,] 3.407 0.8035 2.981 5.269 6.989 5.107 7.143 3.127 3.624 7.220 4.805 0.3951
## [2,] 3.907 5.5971 5.488 4.995 6.496 5.980 1.576 3.009 6.605 3.440 2.754 4.0866
## [3,] 1.945 3.7668 4.860 2.945 3.663 5.945 7.168 2.012 5.873 8.190 7.441 5.9293
## [4,] 4.893 6.2054 4.403 3.967 2.880 7.196 1.813 3.283 5.216 5.699 2.829 6.4729
## [5,] 5.706 0.9084 5.802 1.404 3.122 1.866 6.613 3.299 4.990 3.645 3.766 2.7172
Run Code Online (Sandbox Code Playgroud)

谢谢!

A5C*_*2T1 44

添加类似于options(width=120)文档的内容将允许您覆盖默认的包装宽度.

但是要小心太宽; 转换为PDF或其他格式时,默认情况非常正确!

例如,我使用KnitrRStudio,并将我的文档键入R markdown文档.我的文档" options"在开头可能是这样的:

```{r set-options, echo=FALSE, cache=FALSE}
options(width=80)
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, echo = TRUE, tidy = TRUE, size="small")
read_chunk("some/script/I/want/to/load.R")
```
Run Code Online (Sandbox Code Playgroud)

  • 当我设置`options(width = X)`时,在RStudio中编译报表似乎没有效果.我正在使用RStudio 0.99.467和knitr 1.11.之前在`options`中设置`width`按预期工作.有什么变化(或是用户错误)?我的输出固定大约120个字符. (7认同)
  • @susjoh是预期的; `options('width')`不适用于文本输出宽度_precisely_,所以有时你需要临时调整某个块的`width`选项 (2认同)
  • 没关系,它通过设置`options(width=100)` 起作用。谢谢! (2认同)