r - 根据 tibble 中的数据发送电子邮件

标签 r purrr sendmailr

我正在尝试迭代 tibble 以从每一行发送电子邮件,但无法使其工作。这是示例:

library(tidyverse)
library(sendmailR)
library(pander)

首先创建我想要包含到电子邮件中的表格

tbl <- tibble(A = c(1,2,3),
              B = c(4,5,6),
              C = c(7,8,9))

table <- pander_return(tbl)

创建标题,每一列对应于我想要包含在电子邮件中的某些信息

emails <- tibble(from = c("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19b909f94df959e94b1969cdf929e9c" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="660c090e084802090326010b4805090b" rel="noreferrer noopener nofollow">[email protected]</a>"),
                 to = c("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe949f909bd09a919bbe9993d09d9193" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d37323533733932381d3a30733e3230" rel="noreferrer noopener nofollow">[email protected]</a>"),
                 subject = "This is test",
                 greetings = "Happy Christmas",
                 data = list(table, table))

现在我想映射每一列并将其添加到sendmailR包中sendmail函数的正确位置。以下是我如何发送一封电子邮件的示例。唯一有趣的一点是 greetingstable 如何连接在一起以创建 msg 字段。

from <- "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="583239363d763c373d183f35763b3735" rel="noreferrer noopener nofollow">[email protected]</a>"
to <- "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254f444b400b414a406542480b464a48" rel="noreferrer noopener nofollow">[email protected]</a>"
subject <- "This is test"
msg <- c(greetings, table)

sendmailR::sendmail(from = from, to = to, subject = subject, msg = msg)

那么我如何将电子邮件标题映射到 sendmail 函数,以便每行发送电子邮件。

最佳答案

这是 purrr 中的 pmap 函数的完美用例

您可以执行以下操作

pmap( list(emails$from, emails$to, emails$subject, emails$data) 
      , ~sendmailR::sendmail(from = ..1, 
                            to = ..2, 
                            subject = ..3, 
                            msg = ..4))

这会创建一个参数列表,然后使用 ~ 我们定义函数。 ..x 表示参数在输入列表中出现的顺序。

完整代表

library(tidyverse)
library(sendmailR)
library(pander)

tbl <- tibble(A = c(1,2,3),
              B = c(4,5,6),
              C = c(7,8,9))

table <- pander_return(tbl)

emails <- tibble(from = c("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d171c1318531912183d1a10531e1210" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076d686f692963686247606a2964686a" rel="noreferrer noopener nofollow">[email protected]</a>"),
                 to = c("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1278737c773c767d7752757f3c717d7f" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1cbcec9cf8fc5cec4e1c6cc8fc2cecc" rel="noreferrer noopener nofollow">[email protected]</a>"),
                 subject = "This is test",
                 greetings = "Happy Christmas",
                 data = list(greetings, table))

pmap( list(emails$from, emails$to, emails$subject, emails$data) 
      , ~sendmailR::sendmail(from = ..1, 
                            to = ..2, 
                            subject = ..3, 
                            msg = ..4))

只是为了表明它可以与较低风险的函数一起使用:

pmap( list(emails$from, emails$to, emails$subject, emails$data) 
      , ~paste(..1, 
                            ..2, 
                            ..3))

输出:

[[1]]
[1] "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395358575c175d565c795e54175a5654" rel="noreferrer noopener nofollow">[email protected]</a> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a101b141f541e151f3a1d1754191517" rel="noreferrer noopener nofollow">[email protected]</a> This is test"

[[2]]
[1] "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6903060107470d060c290e04470a0604" rel="noreferrer noopener nofollow">[email protected]</a> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb7b2b5b3f3b9b2b89dbab0f3beb2b0" rel="noreferrer noopener nofollow">[email protected]</a> This is test"

关于r - 根据 tibble 中的数据发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59156399/

相关文章:

r - 基于分隔符将快速 data.table 列拆分为多行

r - 如何访问检查包裹时可能出现的任何注释?

r - 列表列上的 purrr pmap 和几个向量

R:purrr:使用 pmap 进行逐行操作,但这次涉及很多列

r - 如何使用 dplyr 计算两个分组变量的加权平均值

r - 可用于 R 中任何存储的 Manipulate 函数

java - Outlook 禁止使用 R 中的 mailR 对某些电子邮件地址进行身份验证

r - 从电子邮件正文中的附件和图像中提取 Zip+CSV 文件

r - 使用 sendemailR 的问题

r - 如何将列表中的字符(0)转换为 NA?