49

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)

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

Thanks!

susjoh
  • 2,298
  • 5
  • 20
  • 20
  • 6
    This isn't really a problem, and it's not `Knitr`-based exactly. If you run `options()` at the console, you would probably see that the last item is `$width`, and on my system, that's set at "`87`", which will lead to this type of wrapping. – A5C1D2H2I1M1N2O1R2T1 Aug 29 '12 at 11:21

1 Answers1

50

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

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

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")
```
Jim G.
  • 15,141
  • 22
  • 103
  • 166
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Thank you for your fast answer. I wanted to adjust the width of the output for ease of reading the report. The text chunks above and below the output were much wider than the wrapped table above, even at print width. – susjoh Aug 29 '12 at 11:28
  • 2
    @susjoh that is expected; `options('width')` does not apply to the text output width _precisely_, so sometimes you need to temporarily adjust the `width` option for a certain chunk – Yihui Xie Aug 29 '12 at 18:04
  • 8
    When I set `options(width=X)` it seems to have no effect when compiling reports in RStudio. I'm using RStudio 0.99.467, and knitr 1.11. Previously setting `width` in `options` worked as expected. Has something changed (or is it user error)? My output is fixed around 120 characters. – geneorama Jan 08 '16 at 17:28
  • I'm having issues with this too. Setting `<>=` doesn't work. – lostsoul29 Jul 30 '16 at 02:47
  • 2
    Never mind, it worked by setting `options(width=100)`. Thanks! – lostsoul29 Jul 30 '16 at 02:48
  • @geneorama: Try setting `tidy = FALSE` in the `opts_chunk$set`section. https://github.com/rstudio/rmarkdown/issues/646 – Jim G. Sep 17 '16 at 05:15