[英]Add gt table and ggplot images to email
I'm trying to add a combination of a gt table and a number of ggplot images to an email for sending to my team.我正在尝试将 gt 表和一些 ggplot 图像的组合添加到 email 以发送给我的团队。
It's my understanding that to include a gt table in an email it needs to be converted to html using as_raw_html()据我了解,要在 email 中包含一个 gt 表,需要使用 as_raw_html() 将其转换为 html
When I do this I can add the table to the email body but the column headers get all messed up.当我这样做时,我可以将表格添加到 email 正文,但列标题会变得一团糟。 When I exclude the as_raw_html I only get the contents of the table and no structure.
当我排除 as_raw_html 时,我只得到表格的内容而没有结构。
Here's a reprex to show what I'm seeing.这是一个代表来展示我所看到的。 Any pointers on how I can get the table and images into the body would be greatly appreciated.
任何关于如何将表格和图像放入正文的指示都将不胜感激。
library(purrr)
library(ggplot2)
library(gt)
library(glue)
library(blastula)
library(dplyr)
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)
}
plots <- map(unique(iris$Species), generate_plots)
# CREATE GT TABLE
summary_table <- gt(iris) %>%
as_raw_html()
# GENERATE THE BODY TEXT
body_text <- md(glue("
Team,
Check these out....
"))
# ADD THE GT TABLE TO THE EMAIL
body_text <- md(glue("{body_text}\n\n{summary_table}"))
# COMBINE ALL THE PLOTS IN THE BODY TEXT
for (p in plots) {
body_text <- md(glue("{body_text}\n\n{p}"))
}
# SIGN OFF THE EMAIL
body_text <- md(glue("{body_text}\n\nThanks,\n\nMyName"))
# COMPOSE THE EMAIL MESSAGE
formatted_email <- compose_email(body = body_text)
formatted_email
Adding the gt package owner in the hope of getting a reply, @Rich Iannone添加 gt package 所有者希望得到回复,@Rich Iannone
It appears as though applying the gt() function to the iris dataset is not enough, specific arguments need to be set.似乎将 gt() function 应用于 iris 数据集是不够的,需要设置特定的 arguments。 I came across this repo from the package creator for the iris data and it solves my problem
我从 package 的虹膜数据创建者那里看到了这个 repo,它解决了我的问题
https://github.com/rstudio/gt/blob/master/tests/gt-examples/01-html-script/html-01-iris.R https://github.com/rstudio/gt/blob/master/tests/gt-examples/01-html-script/html-01-iris.R
The final solution looks as follows:最终解决方案如下所示:
library(purrr)
library(ggplot2)
library(gt)
library(glue)
library(blastula)
library(dplyr)
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)
}
plots <- map(unique(iris$Species), generate_plots)
# CREATE GT TABLE
summary_table <- gt(iris) %>%
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).")
) %>%
as_raw_html()
# GENERATE THE BODY TEXT
body_text <- glue("
Team,
Check these out....
")
# ADD THE GT TABLE TO THE EMAIL
body_text <- glue("{body_text}\n\n{summary_table}")
# COMBINE ALL THE PLOTS IN THE BODY TEXT
for (p in plots) {
body_text <- glue("{body_text}\n\n{p}")
}
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.