cssrr-markdown

Add horizontal scrollbar to avoid truncated output


I am trying to print a large table in R Markdown and for some reason the output is split up which makes it hard to read.

I would like to add an horizontal scrollbar so it doesn't split. I managed to do it for the vertical axis, but not for the horizontal one.

Here is a sample of code:

---
title: "Test"
output: html_document
---

```{css, echo=FALSE}
.main-container {
    max-width: 800px !important;
}

pre {
  max-height: 800px !important;
  overflow-y: auto !important;
  overflow-x: scroll !important;
}
```

```{r}
mdl <- lm(data = iris, Petal.Width ~ Sepal.Length*Sepal.Width*Petal.Length*Species)
summary(mdl)
```

This is how the output looks like: enter image description here

As you can see, the horizontal scroll bar shows-up but the output is still split-up. I tried with both overflow-x: scroll !important; and max-width: 500px !important; without success.

The problem persists even if I set the .main-container CSS to max-width: 1200px !important; so that there should in principle be enough space for the model to print without splitting-up.

enter image description here

This is what I'm looking for:

enter image description here


Update
As suggested by @Rorschach, I have added the options(width = 160) to the script. This solves the issue in the second case, that is, when the .main-container CSS max-width is large enough (e.g. 800px):

enter image description here

However, if the max-width is not large enough (e.g. 600px) the print is still messed-up:

---
title: "Test"
output: html_document
---

```{css, echo=FALSE}
.main-container {
    max-width: 600px !important;
}

pre {
  max-height: 800px !important;
  overflow-y: auto !important;
  overflow-x: scroll !important;
}
```

```{r setup}
options(width = 160)
```

```{r}
mdl <- lm(data = iris, Petal.Width ~ Sepal.Length*Sepal.Width*Petal.Length*Species)
summary(mdl)
```

enter image description here


Solution

  • Not an css expert but the following seems to do the trick:

    ---
    title: "Test"
    output: html_document
    ---
    
    ```{css, echo=FALSE}
    .main-container {
        max-width: 600px !important;
    }
    
    pre {
      max-height: 800px !important;
      overflow-y: auto !important;
      overflow-x: scroll !important;
    }
    pre code {
      white-space: pre
    }
    ```
    
    ```{r}
    options(width=160)
    mdl <- lm(data = iris, Petal.Width ~ Sepal.Length*Sepal.Width*Petal.Length*Species)
    summary(mdl)
    ```