Colorful tables in a terminal
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
It all started when I wanted to have significant p-values shown on the terminal colored in red. The R terminal is capable of showing colors, simple formatting (like italics or bold) and Unicode characters, thanks to the actual terminal that does the job of displaying R output – whether it is the console of rstudio or a terminal window. You can see that when you use tibbles from tidyverse: they use some very limited formatting (like showing “NA” in red).
I ended up writing a new package, colorDF. The package defines a new class of data frames, but it really does not change their behavior – just the way they are shown (specifically, it modifies some attributes and introduces a print.colorDF
function for printing). If you change a tibble to a colorDF, it will still behave exactly like a tibble, but it will be shown in color:
# Color data frame 6 x 87: # (Showing rows 1 - 20 out of 87) │name │height│mass │birth_year│gender │probability 1│ Luke Skywalker│ 172│ 77│ 19│male │0.0083 2│ C-3PO│ 167│ 75│ 112│NA │0.0680 3│ R2-D2│ 96│ 32│ 33│NA │0.0596 4│ Darth Vader│ 202│ 136│ 42│male │0.0182 5│ Leia Organa│ 150│ 49│ 19│female │0.0138 6│ Owen Lars│ 178│ 120│ 52│male │0.0115 7│ Beru Whitesun lars│ 165│ 75│ 47│female │0.0489 8│ R5-D4│ 97│ 32│ NA│NA │0.0040 9│ Biggs Darklighter│ 183│ 84│ 24│male │0.0954 10│ Obi-Wan Kenobi│ 182│ 77│ 57│male │0.0242 11│ Anakin Skywalker│ 188│ 84│ 42│male │0.0066 12│ Wilhuff Tarkin│ 180│ NA│ 64│male │0.0605 13│ Chewbacca│ 228│ 112│ 200│male │0.0587 14│ Han Solo│ 180│ 80│ 29│male │0.0519 15│ Greedo│ 173│ 74│ 44│male │0.0204 16│Jabba Desilijic Tiure│ 175│ 1358│ 600│hermaphrodite│0.0929 17│ Wedge Antilles│ 170│ 77│ 21│male │0.0457 18│ Jek Tono Porkins│ 180│ 110│ NA│male │0.0331 19│ Yoda│ 66│ 17│ 896│male │0.0931 20│ Palpatine│ 170│ 75│ 82│male │0.0012
Yes, it looks like that in the terminal window!
You can read all about it in the package vignette (please use the package from github, the CRAN version is lagging behind). Apart from the print
function, I implemented also a summary
function which is more informative than the default summary function for the data frames.
starwars %>% as.colorDF %>% summary # Color data frame 5 x 13: │Col │Class│NAs │unique│Summary 1│name │<chr>│ 0│ 87│All values unique 2│height │<int>│ 6│ 45│ 66 [167 <180> 191] 264 3│mass │<dbl>│ 28│ 38│ 15.0 [ 55.6 < 79.0> 84.5] 1358.0 4│hair_color│<chr>│ 5│ 12│none: 37, brown: 18, black: 13, white: 4, blond: 3, auburn: 1, … 5│skin_color│<chr>│ 0│ 31│fair: 17, light: 11, dark: 6, green: 6, grey: 6, pale: 5, brown… 6│eye_color │<chr>│ 0│ 15│brown: 21, blue: 19, yellow: 11, black: 10, orange: 8, red: 5, … 7│birth_year│<dbl>│ 44│ 36│ 8 [ 35 < 52> 72] 896 8│gender │<chr>│ 3│ 4│male: 62, female: 19, none: 2, hermaphrodite: 1 9│homeworld │<chr>│ 10│ 48│Naboo: 11, Tatooine: 10, Alderaan: 3, Coruscant: 3, Kamino: 3, … 10│species │<chr>│ 5│ 37│Human: 35, Droid: 5, Gungan: 3, Kaminoan: 2, Mirialan: 2, Twi'l… 11│films │<lst>│ 0│ 24│Attack of the Clones: 40, Revenge of the Sith: 34, The Phantom … 12│vehicles │<lst>│ 0│ 11│Imperial Speeder Bike: 2, Snowspeeder: 2, Tribubble bongo: 2, A… 13│starships │<lst>│ 0│ 17│Millennium Falcon: 4, X-wing: 4, Imperial shuttle: 3, Naboo fig…
For numeric vectors, by default the function shows the minimum, quartiles and median, but it can also produce a boxplot-like graphical summary. Since the function works also on lists, implementing a text terminal based boxplot function was super easy:
term_boxplot(Sepal.Length ~ Species, data=iris, width=90) # Color data frame 5 x 4: │Col │Class│NAs │unique│Summary 1│setosa │<dbl>│ 0│ 15│╾──────┤ + ├────────╼ 2│versicolor│<dbl>│ 0│ 21│ ╾─────────┤ + ├──────────╼ 3│virginica │<dbl>│ 0│ 21│ ╾──────────────────┤ + ├──────────────╼ 4│Range │<chr>│ 0│ 1│Only one value: Range: 4.3 - 7.9
Cool, isn’t it?
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.