Reputation: 1821
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!
Upvotes: 7
Views: 4528
Reputation: 7790
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.
Upvotes: 7