Is there a way to embed a ggplot image dynamically by row (like a sparkline) using the gt package?
I think I may have found a solution by changing some of the code.
library(ggplot2)
library(gt)
# make a plot of each mfr
tibble_plot <- gtcars %>%
group_by(mfr) %>%
nest() %>%
mutate(plot = map(data, ~ggplot(., aes(hp, trq, size = msrp)) + #you could use the function and it should work
geom_point() +
geom_point(color = "blue") +
theme(legend.position = "none"))) %>%
select(-data) %>%
# Create empty column (a placeholder for the images)
mutate(ggplot = NA)
#Creates the length of the tibble
text_names <- gtcars %>%
select(mfr) %>%
unique() %>%
pull()
# Is a bit slow for me
tibble_output <- tibble(
text = text_names,
ggplot = NA,
.rows = length(text_names)) %>%
gt() %>%
text_transform(
locations = cells_body(vars(ggplot)),
fn = function(x) {
map(tibble_plot$plot, ggplot_image, height = px(200))
}
)
tibble_output