This post explains how to create and style
tables using the kableExtra
.
This package uses the
kable()
function from knitr
to generate table
output and add features on top of it by adding layers.
We’ll showcase the key features of
kableExtra
and provides a set of table
examples using the package.
{kableExtra}
The kableExtra
package offers style features from
html and css with a syntax based on the pipe symbol
%>%
.
The kable()
function comes from knitr
, a
package designed for dynamic report generation in R.
It will generate a table from a data frame and the
kableExtra
package will add features on top of it, such as
adding images, footnotes, HTML
tags, URL links (look at the
Sepal.Width
column on the right), and so
on within table cells.
✍️ author → Hao Zhu
📘 documentation → github
⭐️ more than 500 stars on github
To get started with kableExtra
, you can install it
directly from CRAN using the install.packages
function:
The kableExtra
package has multiple themes:
kable_paper
, kable_classic
,
kable_classic_2
, kable_minimal
,
kable_material
and kable_material_dark
.
Here’s a basic example with the dark one:
library(kableExtra)
data(iris)
head(iris) %>%
kbl() %>%
kable_styling() %>%
kable_material_dark(c("striped", "hover"))
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3.0 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5.0 | 3.6 | 1.4 | 0.2 | setosa |
5.4 | 3.9 | 1.7 | 0.4 | setosa |
You can personalized every color aspect of the table. In this example we show 5 different ways to apply a color, a gradient of colors or a list of colors to kable columns.
Example:
library(kableExtra)
data(iris)
df = iris[order(iris$Petal.Length, decreasing = T),] # sort by petal length value
# gradient color
colfunc = colorRampPalette(c("purple", "orange"))
n_color = 8
colors = colfunc(n_color) # generate 8 colors, from purple to orange
# generate the table
head(df, 8) %>%
kbl(align = "c") %>% # center the columns
kable_styling() %>%
kable_paper(c("striped", "hover")) %>% # add strips and hover
column_spec(2, color='red') %>%
column_spec(3, background=colors) %>%
column_spec(4, color=colors) %>%
column_spec(5, background='lightblue') %>%
column_spec(6, color=c("pink","pink","pink","yellow","pink","green","pink","pink"))
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
119 | 7.7 | 2.6 | 6.9 | 2.3 | virginica |
118 | 7.7 | 3.8 | 6.7 | 2.2 | virginica |
123 | 7.7 | 2.8 | 6.7 | 2.0 | virginica |
106 | 7.6 | 3.0 | 6.6 | 2.1 | virginica |
132 | 7.9 | 3.8 | 6.4 | 2.0 | virginica |
108 | 7.3 | 2.9 | 6.3 | 1.8 | virginica |
110 | 7.2 | 3.6 | 6.1 | 2.5 | virginica |
131 | 7.4 | 2.8 | 6.1 | 1.9 | virginica |
kableExtra
allows you to add images and url links
directly into columns.
Example:
library(kableExtra)
df_img = data.frame(name = c("Grace Wahba","Ragnar Frisch"),
field = c("Machine Learning", "Econometrics"),
image = ""
)
df_img %>%
kbl(booktabs = T, align = "c") %>%
kable_styling() %>%
kable_paper(full_width = T) %>%
column_spec(1, link = c("https://en.wikipedia.org/wiki/Grace_Wahba",
"https://en.wikipedia.org/wiki/Ragnar_Frisch")) %>%
column_spec(2, bold=TRUE) %>%
column_spec(3, image = spec_image(c("https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Grace_Wahba_1986.jpg/440px-Grace_Wahba_1986.jpg",
"https://upload.wikimedia.org/wikipedia/commons/a/a1/Uio_frisch_2006_0025.jpg"),
200, 200) # dimensions of the images
)
name | field | image |
---|---|---|
Grace Wahba | Machine Learning | |
Ragnar Frisch | Econometrics |
The kableExtra
package proposes 3 plotting tools:
spec_hist
, spec_boxplot
, and
spec_plot
. We split the iris dataset according to the
Species
column.
Example:
library(kableExtra)
data(iris)
df = data.frame(Species = c('Setosa', 'Versicolor', 'Virginica'),
boxplots = "", histograms = "", plots1 = "", plots2 = "")
sepal_length_list = split(iris$Sepal.Length, iris$Species)
df %>%
kbl(booktabs = TRUE) %>%
kable_styling() %>%
kable_paper(full_width = TRUE) %>%
column_spec(2, image = spec_boxplot(sepal_length_list)) %>%
column_spec(3, image = spec_hist(sepal_length_list)) %>%
column_spec(4, image = spec_plot(sepal_length_list, same_lim = TRUE)) %>%
column_spec(5, image = spec_plot(sepal_length_list, polymin = 5))
Species | boxplots | histograms | plots1 | plots2 |
---|---|---|---|---|
Setosa | ||||
Versicolor | ||||
Virginica |
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥