Introducing 'emphatic' - highlight data.frames in the R console


Lifecycle: experimental R build status

{emphatic} augments the output of data.frames and matrices in R by adding user-defined ANSI highlighting.

See the online documentation for vignettes and more examples.

What’s in the box

There are separate high-level functions for highlighting data.frames and matrices, and a low-level function which can be used on both.

The hl_ prefix can be read as highlight.

  • hl() for highlighting data.frames
  • hl_matrix() for highlighting matrices
  • hl_loc() for low-level control of highlighting of both data.frames and matrices
  • hl_opt() to set some local options on the current emphatic object e.g. full_colour option sets 24-bit colour mode.
  • hl_opt_global() sets global options for highlighting. These values will be the default unless overridden with a call to hl_opt() for the given emphatic object.
data.frame matrix
High Level hl() hl_matrix()
Low Level hl_loc() hl_loc()


You can install from GitHub with:

# install.package('remotes')


  • This package calls eval() on user-supplied code and extreme caution should be taken before exposing functions in this package to the internet (e.g. via shiny)

Example - set alternating colours on a data.frame

emphatic::hl_opt_global(dark_mode = FALSE)

mtcars %>%
  hl(c('red', 'white')) 

Example 2 - using row and column specification

Use {emphatic} to highlight the mtcars dataset where:

  • 6 and 8 cylinder cars only
  • colour each row to indicate the miles-per-gallon rating
  • do not colour the gear or carb columns
  • highlight the car with the maximum miles per gallon in hotpink
  • de-emphasise the numeric values to focus on the colour highlighting
mtcars %>%
  hl(ggplot2::scale_colour_viridis_c(), rows = cyl %in% c(6, 8), 
     cols = mpg, dest_cols = mpg:am) %>%
  hl('hotpink', rows = mpg == max(mpg)) %>%
  hl_opt(text_contrast = 0.25)

Example 3 - Rainbows!

mtcars %>% 
  hl(rainbow(32)) %>%
  hl_opt(text_contrast = 0.5)

Example 4: Correlation matrix

Create a correlation matrix of some of the variables in mtcars.

Colour the values using red for negative correlations and blue for positive correlations. Values in-between are coloured using a gradient between red and blue. This colouring is applied using ggplot2::scale_colour_gradient2().

mtcars %>%
  select(cyl, mpg, hp, disp, vs) %>%
  cor() %>%
  hl_matrix(scale_colour_gradient2(), selection = abs(.x) > 0.7 & row(.x) != col(.x))