Kable (kableExtra) vertical scrollbar in knitted RMarkdown html_document? - r

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
But after knitting, the vertical scrollbar is gone and the whole data.frame is printed
How can I get the vertical scrollbar to display in the knitted HTML the same as it was in RStudio?

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

Related

Set internal document link as table value in Rmarkdown

I would like to set each value of a column in a datatable as an internal link to a different slide. Below is what I have tried but the link is not selectable.
---
title: "Example"
output: slidy_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r message=FALSE, warning=FALSE, include=FALSE}
library(DT)
library(tidyverse)
```
## Table
```{r carstab, echo = FALSE}
datatable(mtcars %>% rownames_to_column(var = "plot") %>% mutate(plot = ("[plot](#/plot)")),escape = TRUE)
```
## plot
```{r plot, echo = TRUE}
plot(mtcars)
```
mutate(plot = ("[plot](#/plot)")) does not work because the markdown code [plot](#/plot) will not be translated to HTML. Its a character.
You want something like <a href = '#(slide_number)'> This is a link </a> in your DT cells.
You could generate such links using paste0():
datatable(
mtcars %>%
mutate(
plot = 3,
plot = paste0("<a href = '#(", plot, ")'>link</a>"),
), escape = FALSE
)
The plot column in your DT now contains valid links to slide 3 (modify the first instance of plot in mutate() accordingly to link to different slides).

R markdown: how to print dataframe compactly

I am using knitr::kable to print my dataframes, but sometimes they are too big. Is there any simple way to print them compactly with scrollbar?
For example, I do:
knitr::kable(mtcars)
How could I add scrolling by condition (for example, if nrow > 10 and/or ncol > 10)?
P.S. DT::datatable doesn't work for big ncol:
I need exactly scrolling interface.
You can add scrollbars. For example, with kableExtra or DT:
R Markdown
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
Some very wide data:
```{r}
df <- cbind(mtcars, mtcars)
```
With `kableExtra`:
```{r}
kable(df) %>%
kable_styling("striped", full_width = F) %>%
scroll_box(width = "100%", height = "200px")
```
Or with `DT`:
```{r}
DT::datatable(
df,
height = 200,
options = list(scrollX = TRUE)
)
```
output

can't align rows with kableExtra `cell_spec()`

---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r mtcars, warning=FALSE, message=FALSE, echo=FALSE, results='asis'}
library(tidyverse)
library(kableExtra)
kable(mtcars %>%
select(1:5) %>%
head(10) %>%
mutate(cyl = cell_spec(cyl, align = "r")),
"html",
escape = FALSE) %>%
kable_styling("striped", "hover", full_width = TRUE) %>%
row_spec(c(1, 3), background = "yellow")
```
I follow the KableExtra guide to align cells using the cell_spec() function. I am trying to right align my cyl column. But notice the column is not right aligned (image below).
What went wrong?
Jason, the kable function itself has an align option. I think if you are not looking for any conditional alignment, you should just use that.
library(kableExtra)
library(tidyverse)
mtcars %>%
select(1:5) %>%
head(10) %>%
kable(align = c("crcccc")) %>%
kable_styling("striped", "hover", full_width = TRUE) %>%
row_spec(c(1, 3), background = "yellow")
For cell_spec itself, it seems like right now this align option for HTML just won't give you what you need. If you check the raw HTML generated by this approach, you will see ultimately, cell_spec puts your cell into a <span> tag. You can specify text_align there but that gets overrided by the text_align setting in <td>. I will see if I should fix this or add a warning note here in this section.
<td style="text-align:left;"> <span style=" text-align: right;">4</span> </td>

how to automatically rename columns with math mode?

Consider this simple example
---
title: "Untitled"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")
```
## Slide with R Output
```{r , echo = FALSE ,warning = FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
cars %>%
filter(dist < 5) %>%
kable('latex', booktabs = T, escape = F, col.names = c( "$\\alpha$" , "$\\beta$" ) ) %>%
kable_styling(latex_options = c("striped", "hold_position"),
full_width = T)
```
Now this correcly generates the following output
The issue is that the renaming in col.names is manual and very tedious when my dataframe has many columns.
I would like to be able to able to say " if you see this variable dist in the dataframe, then map it to $\alpha$. Otherwise leave as is.
How can this be done?
Note, I am rendering the file using rmarkdown::render()
Thanks!!

For Loop for Creating Multiple Tables using knitr and kableExtra packages in RMarkdown

I need to create multiple tables in RMarkdown and style it with the kableExtra package. As an example, I have the iris dataset. My first table displays the first 20 rows, my second table the next 20 rows, and my third table next 20 rows... Below is the code:
---
title: ""
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
landscape(kable_styling(kable(iris[1:20, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[21:40, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[41:60, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
```
It works well and it returns three tables, each one in a different sheet. In reality I have more than just three tables so I thought it would be a lot wiser to use a for loop and I made use of the answer given in this link R: why kable doesn't print inside a for loop?. Simple I put a line break after each print call as advised.
---
title: "untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
cat("\n")
}
```
But it does not work. I guess that is because I encapsulated the kable command with the commands from the kableExtra package.
Is there who can make it work? I mean is there a way that can save me from typing?
Your existing code was nearly there. I think the only change needed is to add results='asis' to the chunk option (I don't think the extra newline is necessary). Here's the full RMarkdown content which works for me
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```

Resources