[英]Add gt table and ggplot images to email
我正在嘗試將 gt 表和一些 ggplot 圖像的組合添加到 email 以發送給我的團隊。
據我了解,要在 email 中包含一個 gt 表,需要使用 as_raw_html() 將其轉換為 html
當我這樣做時,我可以將表格添加到 email 正文,但列標題會變得一團糟。 當我排除 as_raw_html 時,我只得到表格的內容而沒有結構。
這是一個代表來展示我所看到的。 任何關於如何將表格和圖像放入正文的指示都將不勝感激。
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
添加 gt package 所有者希望得到回復,@Rich Iannone
似乎將 gt() function 應用於 iris 數據集是不夠的,需要設置特定的 arguments。 我從 package 的虹膜數據創建者那里看到了這個 repo,它解決了我的問題
https://github.com/rstudio/gt/blob/master/tests/gt-examples/01-html-script/html-01-iris.R
最終解決方案如下所示:
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.