添加 gt 表格和 ggplot 图像到邮件中。

huangapple go评论113阅读模式
英文:

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图像组合到一封邮件中,以便发送给我的团队。

It's my understanding that to include a gt table in an email it needs to be converted to html using as_raw_html()

我理解要在邮件中包含一个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. 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.

这里有一个reprex来展示我看到的情况。如果有关如何将表格和图像放入正文的任何提示,将不胜感激。

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包的所有者Rich Iannone加入进来。

英文:

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.

It's my understanding that to include a gt table in an email it needs to be converted to html using as_raw_html()

When I do this I can add the table to the email body but the column headers get all messed up. When I exclude the as_raw_html I only get the contents of the table and no structure.

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.

  1. library(purrr)
  2. library(ggplot2)
  3. library(gt)
  4. library(glue)
  5. library(blastula)
  6. library(dplyr)
  7. generate_plots &lt;- function(species) {
  8. subset_data &lt;- iris %&gt;%
  9. filter(Species == species)
  10. bar_plot &lt;- ggplot(subset_data, aes(x = Sepal.Length)) +
  11. geom_bar(stat = &quot;count&quot;) +
  12. theme_minimal()
  13. scatter_plot &lt;- ggplot(subset_data, aes(x = Sepal.Length, y = Sepal.Width)) +
  14. geom_point() +
  15. theme_minimal()
  16. box_plot &lt;- ggplot(subset_data, aes(y = Sepal.Length)) +
  17. geom_boxplot() +
  18. theme_minimal()
  19. combined_charts &lt;- cowplot::plot_grid(bar_plot, scatter_plot, box_plot, axis = &quot;l&quot;, ncol = 1, align = &quot;v&quot;, rel_heights = c(5,5,5))
  20. blastula::add_ggplot(plot_object = combined_charts, width = 7, height = 10)
  21. }
  22. plots &lt;- map(unique(iris$Species), generate_plots)
  23. # CREATE GT TABLE
  24. summary_table &lt;- gt(iris) %&gt;%
  25. as_raw_html()
  26. # GENERATE THE BODY TEXT
  27. body_text &lt;- md(glue(&quot;
  28. Team,
  29. Check these out....
  30. &quot;))
  31. # ADD THE GT TABLE TO THE EMAIL
  32. body_text &lt;- md(glue(&quot;{body_text}\n\n{summary_table}&quot;))
  33. # COMBINE ALL THE PLOTS IN THE BODY TEXT
  34. for (p in plots) {
  35. body_text &lt;- md(glue(&quot;{body_text}\n\n{p}&quot;))
  36. }
  37. # SIGN OFF THE EMAIL
  38. body_text &lt;- md(glue(&quot;{body_text}\n\nThanks,\n\nMyName&quot;))
  39. # COMPOSE THE EMAIL MESSAGE
  40. formatted_email &lt;- compose_email(body = body_text)
  41. formatted_email

Adding the gt package owner in the hope of getting a reply, @Rich Iannone

答案1

得分: 0

似乎仅将gt()函数应用于鸢尾花数据集是不够的,需要设置特定的参数。我找到了这个来自鸢尾花数据包创建者的存储库,它解决了我的问题。

https://github.com/rstudio/gt/blob/master/tests/gt-examples/01-html-script/html-01-iris.R

最终的解决方案如下:

  1. library(purrr)
  2. library(ggplot2)
  3. library(gt)
  4. library(glue)
  5. library(blastula)
  6. library(dplyr)
  7. generate_plots <- function(species) {
  8. subset_data <- iris %>%
  9. filter(Species == species)
  10. bar_plot <- ggplot(subset_data, aes(x = Sepal.Length)) +
  11. geom_bar(stat = "count") +
  12. theme_minimal()
  13. scatter_plot <- ggplot(subset_data, aes(x = Sepal.Length, y = Sepal.Width)) +
  14. geom_point() +
  15. theme_minimal()
  16. box_plot <- ggplot(subset_data, aes(y = Sepal.Length)) +
  17. geom_boxplot() +
  18. theme_minimal()
  19. combined_charts <- cowplot::plot_grid(bar_plot, scatter_plot, box_plot, axis = "l", ncol = 1, align = "v", rel_heights = c(5,5,5))
  20. blastula::add_ggplot(plot_object = combined_charts, width = 7, height = 10)
  21. }
  22. plots <- map(unique(iris$Species), generate_plots)
  23. # 创建 GT 表格
  24. summary_table <- gt(iris) %>%
  25. tab_spanner_delim(delim = ".") %>%
  26. cols_move_to_start(columns = Species) %>%
  27. fmt_number(
  28. columns = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
  29. decimals = 1
  30. ) %>%
  31. tab_header(
  32. title = md("The **iris** dataset"),
  33. subtitle = md("[All about *Iris setosa*, *versicolor*, and *virginica*]")
  34. ) %>%
  35. tab_source_note(
  36. source_note = md("The data were collected by *Anderson* (1935).")
  37. ) %>%
  38. as_raw_html()
  39. # 生成正文文本
  40. body_text <- glue("
  41. Team,
  42. Check these out....
  43. ")
  44. # 将 GT 表格添加到电子邮件
  45. body_text <- glue("{body_text}\n\n{summary_table}")
  46. # 将所有图表组合到正文文本中
  47. for (p in plots) {
  48. body_text <- glue("{body_text}\n\n{p}")
  49. }
  50. # 结束电子邮件
  51. body_text <- glue("{body_text}\n\nThanks,\n\nMyName")
  52. # 组成电子邮件消息
  53. formatted_email <- compose_email(body = md(body_text))
  54. formatted_email
英文:

It appears as though applying the gt() function to the iris dataset is not enough, specific arguments need to be set. I came across this repo from the package creator for the iris data and it solves my problem

https://github.com/rstudio/gt/blob/master/tests/gt-examples/01-html-script/html-01-iris.R

The final solution looks as follows:

  1. library(purrr)
  2. library(ggplot2)
  3. library(gt)
  4. library(glue)
  5. library(blastula)
  6. library(dplyr)
  7. generate_plots &lt;- function(species) {
  8. subset_data &lt;- iris %&gt;%
  9. filter(Species == species)
  10. bar_plot &lt;- ggplot(subset_data, aes(x = Sepal.Length)) +
  11. geom_bar(stat = &quot;count&quot;) +
  12. theme_minimal()
  13. scatter_plot &lt;- ggplot(subset_data, aes(x = Sepal.Length, y = Sepal.Width)) +
  14. geom_point() +
  15. theme_minimal()
  16. box_plot &lt;- ggplot(subset_data, aes(y = Sepal.Length)) +
  17. geom_boxplot() +
  18. theme_minimal()
  19. combined_charts &lt;- cowplot::plot_grid(bar_plot, scatter_plot, box_plot, axis = &quot;l&quot;, ncol = 1, align = &quot;v&quot;, rel_heights = c(5,5,5))
  20. blastula::add_ggplot(plot_object = combined_charts, width = 7, height = 10)
  21. }
  22. plots &lt;- map(unique(iris$Species), generate_plots)
  23. # CREATE GT TABLE
  24. summary_table &lt;- gt(iris) %&gt;%
  25. tab_spanner_delim(delim = &quot;.&quot;) %&gt;%
  26. cols_move_to_start(columns = Species) %&gt;%
  27. fmt_number(
  28. columns = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
  29. decimals = 1
  30. ) %&gt;%
  31. tab_header(
  32. title = md(&quot;The **iris** dataset&quot;),
  33. subtitle = md(&quot;[All about *Iris setosa*, *versicolor*, and *virginica*]&quot;)
  34. ) %&gt;%
  35. tab_source_note(
  36. source_note = md(&quot;The data were collected by *Anderson* (1935).&quot;)
  37. ) %&gt;%
  38. as_raw_html()
  39. # GENERATE THE BODY TEXT
  40. body_text &lt;- glue(&quot;
  41. Team,
  42. Check these out....
  43. &quot;)
  44. # ADD THE GT TABLE TO THE EMAIL
  45. body_text &lt;- glue(&quot;{body_text}\n\n{summary_table}&quot;)
  46. # COMBINE ALL THE PLOTS IN THE BODY TEXT
  47. for (p in plots) {
  48. body_text &lt;- glue(&quot;{body_text}\n\n{p}&quot;)
  49. }
  50. # SIGN OFF THE EMAIL
  51. body_text &lt;- glue(&quot;{body_text}\n\nThanks,\n\nMyName&quot;)
  52. # COMPOSE THE EMAIL MESSAGE
  53. formatted_email &lt;- compose_email(body = md(body_text))
  54. formatted_email

huangapple
  • 本文由 发表于 2023年5月29日 16:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定