18

Question:

What is the current working solution to set the width of r code output in html files? I would like to set width to something big and use a slider in the html output.

options(width = XXX) seems not to work anymore.

Example:

---
title: "Width test"
output:
  html_document:
    theme: default
---
```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      cache = FALSE, tidy = FALSE, size = "small")
```
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 15)
dataM
```

Result:

enter image description here

sessionInfo() output on the screenshot above.

Related:

(options(width = 999) is not working for me)

knitr: How to prevent text wrapping in output?

How to adjust the output width of RStudio Markdown output (to HTML)

Community
  • 1
  • 1
m-dz
  • 2,342
  • 17
  • 29

3 Answers3

30

You can use this to make the pre blocks scroll horizontally if it overflows.

---
title: "Width test"
output:
  html_document:
    theme: default
---

<style>
pre {
  overflow-x: auto;
}
pre code {
  word-wrap: normal;
  white-space: pre;
}
</style>

```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      cache = FALSE, tidy = FALSE, size = "small")
```
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 20)
dataM
```

enter image description here


For a scrollable height, create a container div with a max height and a overflow-y: auto; or overflow-y: scroll;

Similar question/answer

---
title: "Height test"
output:
  html_document:
    theme: default
---

<style>
.pre-scrolly {
  max-height: 150px;
  overflow-y: auto;
}
</style>

<div class='pre-scrolly'>
```{r}
sessionInfo()
```
</div>

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • 1
    Thanks, it worked for me both in Firefox and Chrome, but NOT in the default Rstudio browser, where only the first chunk had the slider as you described... Looks like it is about time to learn some css magic. Let me keep the question open for a while, maybe some other solution would appear. – m-dz Apr 25 '16 at 20:01
  • 1
    @M.D hmm, so you're right. I only tested in safari. change `overflow-wrap` to `word-wrap` – rawr Apr 25 '16 at 20:05
  • 1
    Thanks! Works perfectly now, also in RStudio. – m-dz Apr 25 '16 at 21:03
  • @rawr I am working with regression results, so I don't like having to scroll back and forth to see the coefficients and p-values. Is it possible to make the output wider without using scroll bars? – z_11122 Nov 10 '21 at 18:06
7

You could reset width and max-width using custom CSS e.g. like this:

---
title: "Width test"
output:
  html_document:
    theme: default
---
<style>
.main-container { width: 1200px; max-width:2800px;}
</style>

```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      cache = FALSE, tidy = FALSE, size = "small")

```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 15)
dataM
```

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thanks to you too! Unfortunately, your solution has not worked in any of my browsers: default RStudio's, Firefox or Chrome... – m-dz Apr 25 '16 at 20:08
1

I ran into a similar problem but was using the Stata engine (more info here). In this case it turned out that it wasn't a problem with knitr itself but with my Stata settings.

The trick was to add a Stata-specific block after the initial setup block that sets the line width.

```{r setup_knitr, echo=FALSE, message=FALSE}
# This is the usual setup block needed to set up the Stata Engine
require(knitr)
statapath <- "C:/Program Files (x86)/Stata13/Stata-64.exe"
opts_chunk$set(engine="stata", engine.path=statapath, comment="")
``` 

```{r setup_stata, echo=FALSE,message=FALSE,collectcode=TRUE}
* Now that Stata engine is set up, this block sets up Stata options
set linesize 200
set more off
```
webelo
  • 1,646
  • 1
  • 14
  • 32