Search code examples
rr-markdownknitrkableextra

Kable (kableExtra) vertical scrollbar in knitted RMarkdown html_document?


The vertical scrollbar generated with kableExtra works in RStudio but doesn't work in the knitted html_document.

Example

Take the following Rmd file

---
  output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(dplyr)
library(kableExtra)
```

```{r}
iris %>% 
  kable %>%
  kable_styling("striped", full_width = F)
```

When running the chunk in RStudio, I see a vertical scrollbar

enter image description here

But after knitting, the vertical scrollbar is gone and the whole data.frame is printed

enter image description here

How can I get the vertical scrollbar to display in the knitted HTML the same as it was in RStudio?


Solution

  • Have a look at the function scroll_box() from the package kableExtra.

    ```{r}
    iris %>% 
      kable %>%
      kable_styling("striped", full_width = F) %>% 
     scroll_box(width = "500px", height = "200px")
    ```
    

    To specify the size of the box, see the arguments in the documentation