In my Rmarkdown reports I use package arsenal's tableby()
function to build a table, which I then push through knitr's kable()
and kableExtra's kable_styling()
to create nice-looking tables in a Rmarkdown HTML document. I have many tables and would like the tables to print as condensed as possible in height, i.e., with minimal row height.
To minimize the row height, I've tried decreasing the font_size
, applying the bootstrap_options = c("condensed")
, and changing the CSS for table cell padding.
The problem I encounter is that the indentations/bullet points in the tableby
object seem to create an extra blank space/line below the text in that row.
I'd prefer to continue using kable/kable_extra as it's what I use throughout my Rmarkdown documents, but I'm open to alternatives that work well with arsenal's tableby
tables.
Below is what I've tried:
---
title: "Untitled"
output: html_document
---
# In a CSS chunk
table, th, td {
padding-top: 0px !important;
padding-bottom: 0px !important;
vertical-align: middle;
}
# Chunck options
knitr::opts_chunk$set(echo = TRUE, results = 'asis')
# Packages
library(dplyr)
library(knitr)
library(kableExtra)
library(arsenal)
# Create table object
my_tbl <- tableby(arm ~ age + sex + race + ps + bmi,
data= mock_df, test=FALSE, total=FALSE)
# Push table object through kable and kable_styling
my_tbl %>%
summary(text=TRUE, digits.pct=1, digits=1) %>%
kable() %>%
kable_styling(full_width = FALSE, bootstrap_options = c("condensed"), font_size = 12) %>%
row_spec(2:3, bold = F, extra_css = 'vertical-align: middle !important;')
Below is the result. The rows with the variable names ("Age in years", "sex", "ps") have the row height I'm hoping for, but the rows that start with bullet points seem to have an extra white line or space below. To see where the problem lies I've set vertical alignment in the 2nd and 3rd row to "middle", which only worked for the 2nd, 3rd, and 4th columns, but not the first column with the bullet point.
Any help on getting rid of white space below bullet-point rows is much appreciated.
You can remove extra space and bullet points by tweaking the CSS properties of lists
used in that table, that is, setting margin-bottom
of ul
in table
to 0 and setting list-style
to none
.
(Since you didn't provide a sample data, I have used the built-in dataset mtcars
to show how this works, but this should work for your case too)
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = 'asis')
```
```{css, echo=FALSE}
table, th, td {
padding-top: 0px !important;
padding-bottom: 0px !important;
vertical-align: middle;
}
table, ol, ul {
margin-top: 0;
margin-bottom: 0px;
list-style: none;
}
```
```{r packages, message=FALSE, warning=FALSE}
library(dplyr)
library(knitr)
library(kableExtra)
library(arsenal)
```
```{r create-table}
# Create table object
my_tbl <- tableby(vs ~ cyl + hp + disp + gear,
data= mtcars, test=FALSE, total=FALSE)
```
```{r kable-table}
# Push table object through kable and kable_styling
my_tbl %>%
summary(text=TRUE, digits.pct=1, digits=1) %>%
kable() %>%
kable_styling(full_width = FALSE, bootstrap_options = c("condensed"), font_size = 12) %>%
row_spec(2:3, bold = F, extra_css = 'vertical-align: middle !important;')
```
Now the table looks like