Images as column headers in R

dataViz
Published

March 1, 2020

Have you ever wanted to include an image as a column header in a data frame? Of course you have!

All joking aside, this is actually surprisingly common in corporate environments, where tables may have the company logo in the header, probably as the first ‘column’. You can actually do this in Rmarkdown, with a little help from rlang, tibble knitr and pander. Here’s how.

I’m going to use Google’s logo cos I’m sure they wouldn’t mind. You need to download a copy of this and you’ll need to size it to fit your table. The image below is 115x40 (width x height).

First, load libraries and read in the image with pander, assuming it’s in your working directory:

library(pander)
library(knitr)
library(rlang)
library(tibble)
library(magrittr) # fo' da pipe

img <- "google_logo.png" %>% pander::pandoc.image.return()

You’ll notice this is just markdown: img is "![](google_logo.png)". Ok, rlang, pander and knitr magic coming up 🧙️. The code we’ll use is the following:

tibble(
  {{ img }} := c("Metric One", "Metric Two"),
  `Segment One` = c(100, 250),
  `Segment Two` = c(20, 12)
  ) %>%
  pandoc.table(
    justify = c("left", "right", "right"),
    keep.line.breaks = TRUE
  )

Where { img } uses curly-curly and the walrus operator to access the image variable we’ve created. It gets rendered as markdown thanks to pander’s pandoc.table() and knitr helps with code chunk options. Importantly, these need to include results = 'asis'. So it’ll look like this, minus the #:

#```{r results='asis'}
# amazing code...
#```

So, putting all this together, we get:

tibble(
  {{ img }} := c("Metric One", "Metric Two"),
  `Segment One` = c(100, 250),
  `Segment Two` = c(20, 12),
  `Total for Segments` = c(120, 262)
  ) %>%
  pandoc.table(
    justify = c("left", "right", "right", "right"),
    keep.line.breaks = TRUE
  )


31b8e172-b470-440e-83d8-e6b185028602:dAB5AHAAZQA6AE8AQQBCAGwAQQBHAFkAQQBOAFEAQgBoAEEARABjAEEATgB3AEEAeQBBAEMAMABBAFoAQQBCAGsAQQBEAFkAQQBNAHcAQQB0AEEARABRAEEATgBnAEEAeQBBAEQASQBBAEwAUQBBADQAQQBEAFEAQQBZAGcAQgBtAEEAQwAwAEEAWQBRAEIAbQBBAEQARQBBAE8AUQBBADUAQQBEAFUAQQBZAFEAQQB4AEEARwBJAEEATQBnAEIAaQBBAEQA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QAMABBAEkAZwBCADAAQQBHAFUAQQBlAEEAQgAwAEEAQwAwAEEAWQBRAEIAcwBBAEcAawBBAFoAdwBCAHUAQQBEAG8AQQBJAEEAQgB5AEEARwBrAEEAWgB3AEIAbwBBAEgAUQBBAE8AdwBBAGkAQQBEADQAQQBNAGcAQQAyAEEARABJAEEAUABBAEEAdgBBAEgAUQBBAFoAQQBBACsAQQBBAG8AQQBJAEEAQQBnAEEAQwBBAEEASQBBAEEAOABBAEMAOABBAGQAQQBCAHkAQQBEADQAQQBDAGcAQQBnAEEAQwBBAEEAUABBAEEAdgBBAEgAUQBBAFkAZwBCAHYAQQBHAFEAQQBlAFEAQQArAEEAQQBvAEEAUABBAEEAdgBBAEgAUQBBAFkAUQBCAGkAQQBHAHcAQQBaAFEAQQArAEEAQQA9AD0ACgBzAHUAZgBmAGkAeAA6AA==:31b8e172-b470-440e-83d8-e6b185028602

Now that’s a nice table 😍. And so easy! Keep in mind the styling here is basic, but knitting this in RStudio gives me:

Now that’s a nice table.

You’re not limited to tables by the way. Have a look at my Stack Overflow answer here that shows how easy it is to use images in the table cells. Long story short: