condformat: Conditional formatting for your DataFrames

condformat v0.2.0 has made it into CRAN!

Downloads Twitter URL

condformat helps visualizing DataFrames. With a ggplot2-like syntax we can format columns of our DataFrame and view it directly in the RStudio Viewer (screenshot). See how we combine several formatting rules in this simple example:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.6 2.5 3.9 1.1 versicolor
7 5.9 3.2 4.8 1.8 versicolor
8 6.1 2.8 4.0 1.3 versicolor
9 6.3 2.5 4.9 1.5 versicolor
10 6.1 2.8 4.7 1.2 versicolor
11 6.4 2.9 4.3 1.3 versicolor
12 6.0 2.2 5.0 1.5 virginica
13 6.9 3.2 5.7 2.3 virginica
14 5.6 2.8 4.9 2.0 virginica
15 7.7 2.8 6.7 2.0 virginica
16 6.3 2.7 4.9 1.8 virginica
17 6.7 3.3 5.7 2.1 virginica

If you like ggplot2 I hope you will like condformat syntax too.

data(iris)
library(condformat)
my_df <- iris[c(1:5,70:75, 120:125),]
condformat(my_df) +
  rule_fill_discrete(Species) + 
  rule_fill_discrete(Sepal.Width, Sepal.Length,
                     expression = Sepal.Width > Sepal.Length - 2.25,
                     colours = c("TRUE" = "#7D00FF")) + 
  rule_fill_gradient2(Petal.Length)
  • condformat(my_df): We want to view my_df.
  • Two rule_fill_discrete: Fill column background. Either by value like in Species or filling several columns based on an arbitrary expression (any column or variable can be used in the expression!). Using an arbitrary expression you can specify which colours have to be applied to each of the possible expression results.
  • One rule_fill_gradient2: Fill columns using a gradient with three levels. Here we colour Petal.Length with a three level gradient, going from red to white to blue (the default). Use rule_fill_gradient uses a conventional two-colour gradient.

Only colour background rules are definable right now, but complex expressions can be used! Questions, issues and feature suggestions are very welcome (and pull requests are welcome too!). If you need to set another CSS field, just say it and I'll do my best!

You can install condformat:

install.packages("condformat")

condformat relies on scales for getting great colour scales and htmlTable for rendering the DataFrame into HTML. Thanks to both Hadley Wickham and Max Gordon for their great work!