Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll bar under a wide table not fitting the page width

I am using knitr to produce dynamic documents. If the table is too wide to fit the page (screen) width, is there any way to enforce it fitting the page while keeping a scroll bar under the table so one can slide the bar from left to right and read the table content?

Here is a test code

---
title: "Untitled"
author: "ath"
date: "07/02/2015"
output: 
html_document:
  css:custom.css
---
```{r set-options}
options(width = 80)
```
```{r test, results='markup'}
df.matrix <- matrix(runif(300, min = 0, max = 300), nrow = 2)
df.matrix <- as.data.frame(df.matrix)
colnames(df.matrix) <- paste("col", as.character(seq(1:150)), sep = "")
library("knitr")
kable(df.matrix, col.names = colnames(df.matrix))
```

It looks like the width in the options does not work.

Thanks!

like image 865
athlonshi Avatar asked Jul 02 '15 19:07

athlonshi


1 Answers

You can use the kableExtra to add scrollbars for both vertical and horizontal scrolling.

Example:

---
title: "Untitled"
author: "ath"
date: "07/02/2015"
output: html_document
---

```{r set-options}
library("knitr")
library(kableExtra)
library(magrittr)
options(width = 80)
```

```{r test, results='markup'}
df.matrix <- matrix(runif(300, min = 0, max = 300), nrow = 2)
df.matrix <- as.data.frame(df.matrix)
colnames(df.matrix) <- paste("col", as.character(seq(1:150)), sep = "")
```

```{r kable, results = "asis"}
df.matrix %>%
  kable(format = "html", col.names = colnames(df.matrix)) %>%
  kable_styling() %>%
  kableExtra::scroll_box(width = "100%", height = "100px")
```

<br>

```{r}
devtools::session_info()
```

A screenshot of the table is below. You can get a copy of the example .Rmd file and resulting .html file here.

enter image description here

like image 143
Peter Avatar answered Dec 02 '22 19:12

Peter