Introduction

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.

Installation

#installing packages: dplyr, pander and kableExtra

Getting Started

Here 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]
data



HTML Table Theme

Basic HTML Table Theme

Basic 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


Other HTML Table Theme

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`.


Here’s a preview of each one:

data %>%
  kbl(caption = "Recreating Style Table") %>%
  kable_paper("hover")
Recreating Style Table
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")
Recreating Style Table
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()
Recreating Style Table
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()
Recreating Style Table
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"))
Recreating Style Table
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()
Recreating Style Table
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 Classes

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

  • striped: to add zebra-striping to any table row.
  • bordered: for borders on all sides of the table and cells.
  • hover: to enable a hover state on table rows.
  • condensed: to make tables more compact by cutting cell padding in half.
  • responsive: to make them scroll horizontally on small devices (under 768px). When viewing on anything larger than 768px wide, you will not see any difference in these tables.


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



Further Resources

Learn more about [Creating Tables using kableExtra Package] with the following:



Works Cited

This code through references and cites the following sources:



Thanks for Checking this Out ;)

Sanaz