knitr:如何防止输出中的文字换行? [英] knitr: How to prevent text wrapping in output?
问题描述
编织到HTML时,knitr的代码输出块中的文本换行有问题.
I am having a problem with text wrapping in code output chunks in knitr when knitting to HTML.
例如,如果我运行以下命令:
For example, if I run the following:
matrix(rnorm(60, 5, 2), ncol = 12)
HTML的输出将包装表,给出这样的输出,第12列移到其余的下方:
The output in HTML will wrap the table, giving an output like this, where the 12th column is moved underneath the rest:
## [,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
谢谢!
推荐答案
在文档中添加类似options(width=120)
的内容将使您可以覆盖默认的换行宽度.
Adding something like options(width=120)
to your document would allow you to override the default wrapping width.
但是要注意不要太宽;转换为PDF或其他格式时,默认值几乎是正确的!
Be careful about going too wide though; when converting to PDF or other formats, the default is pretty much just right!
作为示例,我使用RStudio中的Knitr
,然后将我的文档键入为R markdown文档.一开始我的文档"options
"可能是这样的:
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:
```{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")
```
这篇关于knitr:如何防止输出中的文字换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!