knitr:如何防止文本包装输出?

时间:2021-04-13 22:20:45

I am having a problem with text wrapping in code output chunks in knitr when knitting to HTML.

当我编织到HTML时,在knitr的代码输出块中进行文本包装有一个问题。

For example, if I run the following:

例如,如果我运行以下命令:

matrix(rnorm(60, 5, 2), ncol = 12)

The output in HTML will wrap the table, giving an output like this, where the 12th column is moved underneath the rest:

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

Is there a method to adjust the width of the output chunk, so that I can have a table where the rows appear all on one line, like so?

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

##       [,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

Thanks!

谢谢!

1 个解决方案

#1


39  

Adding something like options(width=120) to your document would allow you to override the default wrapping width.

在文档中添加一些选项(宽度=120)可以覆盖默认的包装宽度。

Be careful about going too wide though; when converting to PDF or other formats, the default is pretty much just right!

但是要小心不要太宽;当转换成PDF或其他格式时,默认值是相当正确的!

As an example, I use Knitr from RStudio, and type my document as a R markdown document. My document "options" at the start might be something like this:

例如,我使用来自RStudio的Knitr,并将我的文档作为R标记文档输入。我最初的文档“选项”可能是这样的:

```{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")
```

#1


39  

Adding something like options(width=120) to your document would allow you to override the default wrapping width.

在文档中添加一些选项(宽度=120)可以覆盖默认的包装宽度。

Be careful about going too wide though; when converting to PDF or other formats, the default is pretty much just right!

但是要小心不要太宽;当转换成PDF或其他格式时,默认值是相当正确的!

As an example, I use Knitr from RStudio, and type my document as a R markdown document. My document "options" at the start might be something like this:

例如,我使用来自RStudio的Knitr,并将我的文档作为R标记文档输入。我最初的文档“选项”可能是这样的:

```{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")
```