clitable: a new R package to easily print pretty tables in the terminal

[This article was first published on kforner, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I am pleased to announce that clitable, my new R package for printing tables in the terminal, just made its way to the CRAN!! It lets you print pretty colorful tables directly in the R console or terminal.

I am also quite proud that it was accepted on its first submission, without anything to fix, which is in my experience not that common, and is also the subject of my previous post Preparing Rfuzzycoco for publication on CRAN. The average time for a CRAN submission review is a week, so imagine the delay if you have to resubmit 4 or 5 times. This was made possible by a combination of tools and good practices (and experience): devtools, testthat, roxygen2, covr, git, github actions (CI), rhub, pkgdown, codecov…

And once again, my package has 100% test coverage coverage badge (and yes it’s overkill but I am a Test addict…).

So what is it for ? For pretty printing tables (data frames, matrices) in the terminal, like this: a clitable example

In this example, you can see:

  • column headers, rendered in bold
  • columns with adequate size
  • 2 highlighted rows (in green)
  • NA values rendered with a custom style, strikethrough
  • the flipper_len column with a heatmap background
  • the first value, in cell (1, 1) using a custom crayon style

The corresponding code is:

  # the table to print
  df <- head(datasets::penguins, 20)

  # custom crayon style 
  df$species <- as.character(df$species)
  df[1, 1] <- crayon::style("ADELIE", "underline","bgYellow")

  # create clitable
  ct <- cli_table(df, header_style = "bold",
    NA_style = "strikethrough",
    heatmap_columns = list("flipper_len"), xmin = 180, xmax = 200,
    hilite_rows = !is.na(df$sex) & df$sex == "female" & df$bill_dep >= 19, 
    hilite_style = "bgGreen"
  )

  # print it
  cat(ct, sep = "\n")
}

Note that this is my most complex example.

You may ask, why would I want to print a table in the terminal?? In R we have multiple ways to render pretty tables in html, pdf, images.

My personal use case if for the next version of a package of mine, srcpkgs, which can render the results of testing or checking a collection of R source packages as multiple tables in the terminal. I need to highlight the rows that correspond to errors and to print the elapsed time column as a heatmap to quickly identify bottlenecks. I also wanted to make it compatible with the amazing crayon and cli packages, so that it is easy to customize the output.

Another use case I foresee is for logging some results. The cli package is really useful for that, but seems to be missing an easy way to print tables, hence clitable!

You may also ask, aren’t there other R packages that can render pretty tables as text?? There are indeed. For example I have used a lot the huxtable package, that is really powerful, and can render tables in LaTeX, HTML, text. But using it for my purpose is not that straightforward, to the extent of my knowledge it is not compatible with cli, and it has a lot of dependencies. In comparison, clitable is a dwarf, but has minimal dependencies (crayon/cli), is easy to use and easy to extend, and intended to work well with cli. Note that this is the very first version, and the interface can evolve if needed.

Feedback and contributions are welcome on GitHub.

Here are some more examples, part of clitable::demo()

some styles more styles heatmaps hilite

To leave a comment for the author, please follow the link and comment on their blog: kforner.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)