I'm a little confused on how to use the text_transform
and local_image
function in the gt
package to insert images into a data cell
I have a series of about a dozen .png files with the graphics I'd like to insert. They have names like CA.png, UT.png, OH.png etc. for state abbreviations. They are all together in a local folder.
So given a basic table like
library(gt)
library(magrittr)
Column_one <- c("CA", "UT", "OH")
column_two <- c(NA, NA, NA) #placeholder for graphics
dashboard.data <- data.frame(column_one, column_two, stringsAsFactors = FALSE)
names(dashboard.data)[1] <- "State"
names(dashboard.data)[2] <- "IncidenceGauge"
dboard3 <- dashboard.data %>% gt() %>%
tab_header(
title = md("**Big Title**"),
subtitle = md("*Subtitle*")
) %>%
cols_label(IncidenceGauge = "Risk Level") %>%
print(dboard3)
How would I go about getting the png files loaded into the corresponding rows in column two?
This could be achieved via
gt
functionstext_transform
andlocal_image
like so:text_transform
transforms the content of a column put the filenames in your second column..fn
pass a function totext_transform
to loop over the column elements, loads and transforms the images vialocal_image
and returns a character vector.Making use of
purrr
andggplot2
the following code first makes some example ggplots saves them as png and finally adds them to your second column: