19

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)

3 Answers 3

31

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.D hmm, so you're right. I only tested in safari. change overflow-wrap to word-wrap
Thanks! Works perfectly now, also in RStudio.
@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?
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

1 Comment

Thanks to you too! Unfortunately, your solution has not worked in any of my browsers: default RStudio's, Firefox or Chrome...
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
```

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.