Creating hyperlinks from gt table to corresponding ggplot images

65 Views Asked by At

I'm trying to send an email to my team with a gt table of summary stats and the table is rather large, potentially more than 100 rows. For each row of the summary table I wish to create a plot or two which summarizes the main talking points i.e. scatter plot or bar plot etc. Rather than scrolling through all the plots I'd like to make a link from each row to the corresponding plots.

I've tried creating a regexp using the iris dataset but I don't understand how I can make the references from the gt table to the ggplot images.

I've created the following code which provides a gist of what I'm trying to achieve:

library(dplyr)
library(blastula)
library(glue)
library(gt)

generate_plots <- function(species) {
  
  subset_data <- iris %>% 
    filter(Species == species)
  
  bar_plot <- ggplot(subset_data, aes(x = Sepal.Length)) + 
    geom_bar(stat = "count") +
    theme_minimal()
  
  scatter_plot <- ggplot(subset_data, aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point() +
    theme_minimal()
  
  box_plot <- ggplot(subset_data, aes(y = Sepal.Length)) +
    geom_boxplot() +
    theme_minimal()
  
  combined_charts <- cowplot::plot_grid(bar_plot, scatter_plot, box_plot, axis = "l", ncol = 1, align = "v", rel_heights = c(5,5,5))
  
  blastula::add_ggplot(plot_object = combined_charts,  width = 7, height = 10)
  
  return(combined_charts)  # Return the combined charts object
}

plot_list <- list()

# Store the plot objects and their respective species names
for (species in unique(iris$Species)) {
  plot_obj <- generate_plots(species)
  plot_list[[species]] <- plot_obj
}

# CREATE GT TABLE
summary_table <- iris %>%
  group_by(Species) %>% 
  summarise(across(everything(), mean)) %>%
  gt() %>% 
  tab_spanner_delim(delim = ".") %>%
  cols_move_to_start(columns = Species) %>%
  fmt_number(
    columns = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
    decimals = 1
  ) %>%
  tab_header(
    title = md("The **iris** dataset"),
    subtitle = md("[All about *Iris setosa*, *versicolor*, and *virginica*]")
  ) %>%
  tab_source_note(
    source_note = md("The data were collected by *Anderson* (1935).")
  ) %>%
  cols_label(
    Species = md("Species")
  ) %>%
  as_raw_html()

# GENERATE THE BODY TEXT
body_text <- glue("
Team,

Check these out....

{summary_table}")

# ADD THE PLOT LINKS TO THE EMAIL BODY TEXT
for (species in unique(iris$Species)) {
  plot_obj <- plot_list[[species]]
  plot_link <- glue("<a href='#plot_{species}'>Click here to view the {species} plot</a>")
  body_text <- glue("{body_text}\n\n{plot_link}")
}

# COMBINE ALL THE PLOTS IN THE BODY TEXT
for (species in unique(iris$Species)) {
  plot_obj <- plot_list[[species]]
  plot_html <- blastula::add_ggplot(plot_object = plot_obj, width = 7, height = 10)
  body_text <- glue("{body_text}\n\n<h2 id='plot_{species}'>{species} Plot</h2>\n\n{plot_html}")
}

# SIGN OFF THE EMAIL
body_text <- glue("{body_text}\n\nThanks,\n\nMyName")

# COMPOSE THE EMAIL MESSAGE
formatted_email <- compose_email(body = md(body_text))

formatted_email
0

There are 0 best solutions below