81

I am producing an HTML output but I am having issues with the output width of R code output.

I'm able to adjust the figure width with no difficulty but when I try to write a data table or the factor loadings, R is outputting at a fixed width which is only about a third of my screen width. This results in the columns of the table being split up rather than all of the columns displayed in a single table.

Here is a reproducible example:

---
output: html_document
---

# Title

```{r echo = FALSE, fig.width=16, fig.height=6}
x = matrix(rnorm(100),ncol=10)
x
plot(x)
```

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Matt Weller
  • 2,684
  • 2
  • 21
  • 30
  • I think something like `fig.width = \\linewidth` in the chunk options might help, but I defer to better experts than me – Simon O'Hanlon Mar 13 '13 at 12:49
  • 1
    I'm ok with the figure.width, that can be controlled in my chunk options just fine. The problem is the blocks of text/table output being fixed. Thanks for pointing out the \\linewidth which will be useful in the future. – Matt Weller Mar 13 '13 at 12:53
  • I suggest you post some reproducible sample code to show how the offending plots are created. Maybe if you look in the help manual for your plotting function there will be some dummy data and code samples you can use? It will probably help a lot. – Simon O'Hanlon Mar 13 '13 at 12:55
  • It is not the plots which are offending. The figures are printing just how I would like them. It's the TEXT/TABLES which are not using the full width of the screen. I can supply an example if it is really required. – Matt Weller Mar 13 '13 at 13:06

2 Answers2

118

Add this at the start of your document:

```{r set-options, echo=FALSE, cache=FALSE}
options(width = SOME-REALLY-BIG-VALUE)
```

Obviously, replace SOME-REALLY-BIG-VALUE with a number. But do you really want to do all that horizontal scrolling?

Your output is probably being wrapped somewhere around 80 characters or so.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • That's great, thank you very much. It was even more simple than I imagined! – Matt Weller Mar 13 '13 at 13:11
  • 10
    And notice that the success of this will depend on the css theme. So this will work with Rstudios default theme, and with the cerulian theme, but if you choose the theme *readable* the text will be mangled despite options(width=400). – Rasmus Larsen Nov 19 '15 at 12:41
  • Thanks @A5C1D2H2I1M1N2O1R2T1 – Oshan Aug 14 '18 at 13:37
  • 2
    To provide a solution in the case of the comment by @RasmusLarsen , I was very happy with the inline style in this answer------ . https://stackoverflow.com/a/36846864/1518460 – Mike M Aug 20 '20 at 01:52
29

Also, you can temporarily change the local R options for a code chunk:

```{r my-chunk, R.options = list(width = SOME-BIG-VALUE)}

```
HBat
  • 4,873
  • 4
  • 39
  • 56
  • 2
    Underrated answer. This is more likely to be useful (at least for me) as I usually want to override the sensible defaults only for one particular code chunk. – qdread Mar 10 '22 at 17:56