This code through explores the package kableExtra, which helps to build common complex tables and manipulate table styles. It imports the pipe %>% symbol from dplyer or magrittr packages and verbalize all the functions, so basically you can add “layers” to a kable output in a way that is similar with ggplot2 and plotly.
#installing packages: dplyr, pander and kableExtraHere we are using the first few columns and rows from dataset mtcars using function head(), to create some Examples to view the kableExtra functions in action.
# Load packages
library(dplyr)
library(pander)
library(kableExtra)
# Load data
data <- mtcars[1:5, 1:5]
dataBasic HTML output of kable_styling is just a plain HTML table.
data %>%
kbl() %>%
kable_styling(full_width = F)| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
kableExtra offers a few alternative HTML Table Themes other than the default theme. Right now there are 6 of them:
* `kable_paper`,
* `kable_classic`,
* `kable_classic_2`,
* `kable_minimal`,
* `kable_material` and
* `kable_material_dark`.
data %>%
kbl(caption = "Recreating Style Table") %>%
kable_paper("hover")| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
data %>%
kbl(caption = "Recreating Style Table") %>%
kable_classic(html_font = "Alrai")| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
data %>%
kbl(caption = "Recreating Style Table") %>%
kable_classic_2()| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
data %>%
kbl(caption = "Recreating Style Table") %>%
kable_minimal()| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
data %>%
kbl(caption = "Recreating Style Table") %>%
kable_material(c("striped", "hover"))| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
data %>%
kbl(caption = "Recreating Style Table") %>%
kable_material_dark()| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
These functions are alternatives to kable_styling, which means that you can specify any additional formatting options in kable_styling in these functions too.
Table Style
data %>%
kbl() %>%
kable_styling(full_width = F, bootstrap_options = c("striped", "bordered", "condensed"))| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
Adding Colors
data %>%
kbl() %>%
kable_paper() %>%
column_spec(2, color = "white",
background = spec_color(mtcars$drat[1:2],end = 0.7)) %>%
column_spec(5, color = "yellow",
background = spec_color(mtcars$drat[1:6], end = 0.7),
popover = paste("am:", mtcars$am[1:6])) %>%
row_spec(3, bold = T, color = "white", background = "red") %>%
row_spec(4, bold = T, color = "Darkblue")| mpg | cyl | disp | hp | drat | |
|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 |
Learn more about [Creating Tables using kableExtra Package] with the following:
Resource I The kableExtra package
Resource II Create Awesome LaTeX Table with knitr::kable and kableExtra
Resource III Use kable in Shiny
This code through references and cites the following sources:
Hao Zhu (2021). Source I. Create Awesome HTML Table with knitr::kable and kableExtra
Bootstrap . Source II. fundamental HTML elements styled and enhanced with extensible classes