pillar
pillar provides tools for styling columns of data, artfully using colour and unicode characters to guide the eye.
Installation
# pillar is installed if you install the tidyverse package:
install.packages("tidyverse")
# Alternatively, install just pillar:
install.packages("pillar")
# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("r-lib/pillar")Usage
pillar is a developer-facing package that is not designed for end-users but will eventually be incorporated in packages like tibble.
library(pillar)
x <- 123456789 * (10 ^ c(-3, -5, NA, -8, -10))
pillar(x)
#> [3m[90m<dbl>[39m[23m
#> [4m1[24m[4m2[24m[4m3[24m457.
#> [4m1[24m235.
#> [31mNA[39m
#> 1.23
#> 0.012[4m3[24mIf you render this in a console that supports colour, you’ll see something that looks like this:
Extending
The primary user of this package is tibble, which lets pillar do all the formatting work. Packages that implement a data type to be used in a tibble column can add color with only a few changes:
- Implement the
pillar_shaft()method for your data type. - Add pillar to
Suggestsand implement dynamic method registration usingvctrs::s3_register().- If you don’t mind the dependency, you can also add it to
Imports, and import the methods you override with a regularNAMESPACEimport.
- If you don’t mind the dependency, you can also add it to
tidyverse/hms#43 shows the changes that were necessary to add colored output for the hms package:
pillar.Rfor the actual implementation (old namecolformat.R)DESCRIPTIONfor the dependencyzzz.Rfor the dynamic method registration
Some more detail is given below.
Implementing pillar_shaft.your_class_name()
This method accepts a vector of arbitrary length and is expected to return an S3 object with the following properties:
- It has an attribute
"width" - It can have an attribute
"min_width", if missing,"width"is used - It must implement a method
format(x, width, ...)that can be called with any value betweenmin_widthandwidth- This method must return an object that inherits from
characterand has attributes"align"(with supported values"left","right", and"center") and"width"
- This method must return an object that inherits from
The function new_pillar_shaft() returns such an object, and also
correctly formats NA values. In many cases, the implementation of
pillar_shaft.your_class_name() will format the data as a character
vector (using color for emphasis) and simply call new_pillar_shaft().
See pillar_shaft.numeric() for a code that allows changing the display
depending on the available width.
Useful helpers
style_neg()to format negative valuesstyle_num()to format numbersstyle_subtle()to de-emphasize
Inspirations
The earliest use of unicode characters to generate sparklines appears to be from 2009.
Exercising these ideas to their fullest requires a font with good support for block drawing characters. PragamataPro is one such font.