knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

colorTable

colorTable is a small library that acts as a wrapper around the kabelExtra package that allows the user to create colorful tables.

Currently its mostly used to visualise data manipulation classes by me. The main function of the package is called color_table that takes a data.frame or matrix and different coloring options (see examples below).

Additionally, there are some small helper functions such as view_html() that renders html-code to the viewer and color_gradient() that takes a sequence of numbers and create corresponding color values between two input colors.

Installation

Currently the package lives on GitHub, you can install it with

# install.packages("devtools")
devtools::install_github("DavZim/colorTable")

Usage

library(colorTable)

# use a small subset of the mtcars dataset
df <- data.frame(car = rownames(mtcars), mtcars, stringsAsFactors = F)
rownames(df) <- NULL
(df <- df[1:5, 1:3])

# define some colors for later
red   <- "#fc9272"
green <- "#a6d96a"
blue  <- "#74add1"
color_table(df)

Specify Single Color

color_table(df, header_bg = red, body_bg = blue)

Specify Multiple Colors

# create a sequence of three colors from white to red
(header_bgs <- color_gradient(1:3, c("white", "red")))
color_table(df, header_bg = header_bgs)

Specify Column Color

color_table(df, col_bgs = c(red, green, blue))

Specify Row Color

color_table(df, header_bg = NULL, row_bgs = c(red, blue))

Specify All Colors in a single Matrix

# including the headers
color_mat <- matrix(c(
  "red", "green", "blue",
  color_gradient(1:3, c("red", "white")),
  color_gradient(1:3, c("blue", "white")),
  color_gradient(1:3, c("white", "red")),
  color_gradient(1:3, c("white", "blue")),
  color_gradient(1:3, c("red", "white"))
), ncol = 3, byrow = T)
color_mat
color_table(df, body_bg = color_mat)

Example Case dplyr

Say we want to teach 'dplyr`'s data manipulation functions, we can visualize it with colorTable like so

library(dplyr)
library(dplyr)
# create some colors
cols <- matrix(green, ncol = ncol(df), nrow = nrow(df))
cols

Color MPG

cols1 <- cols
cols1[, 2] <- color_gradient(df$mpg, c("red", "yellow", "green"))
color_table(df, cols1)

Arrange by MPG

# order the color-matrix by the order of mpg
cols1 <- cols1[order(df$mpg), ]
df %>% arrange(mpg) %>% color_table(cols1)

Filter for cyl >= 6

cols2 <- cols
cols2[, ] <- ifelse(df$cyl >= 6, green, red)
color_table(df, cols2)

cols2 <- cols2[df$cyl >= 6, ]
df %>% filter(cyl >= 6) %>% color_table(cols2)

Output Format

The format can be either HTML (format = "html") or pdf (format = "latex"), if used inside of knitr, the format is determined automatically.

The internals are handled by kableExtra and the output should be kableExtra-compatible.

For example the last table rendered in latex looks like this:

# replicating the earier example:
df %>% arrange(mpg) %>% color_table(cols1, format = "latex")



DavZim/colorTable documentation built on May 28, 2019, 2:31 p.m.