通过在 R 中将数据框包含为 table 从 Outlook 发送电子邮件
Send email from outlook by including dataframe as table in R
我有一个数据框,如下所示。然后我将数据帧转换为 html table.
# Use RDCOMClient to send email from outlook
library(RDCOMClient)
# Use xtable to convert dataframe into html table
library(xtable)
# Create dataframe
df <- as.data.frame(mtcars[1:3,1:3])
# Create HTML object
df_html <- xtable(df)
现在,我正在使用电子邮件线程中提供的绝妙解决方案从 outlook 发送电子邮件 Sending email in R via outlook
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "dest@dest.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = df_html
## send it
outMail$Send()
对于我的电子邮件正文,我想要作为 html table 附加的数据框 df。当我执行上面的代码时,我收到以下错误消息。
Error in `[[<-`(`*tmp*`, "body", value = list(mpg = c(21, 21, 22.8), cyl = c(6, :
Can't attach the RDCOMServer package needed to create a generic COM object
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘RDCOMServer’
当我将行 outMail[["body"]] = df_html
更改为 outMail[["body"]] = paste0(df_html)
时,我收到了电子邮件,但输出不是 table。它在我的展望中如下所示。
c(21, 21, 22.8), c(6, 6, 4), c(160, 160, 108)
我希望这是 table。我怎样才能做到这一点?谢谢!
我终于找到了在 Microsoft Outlook 中将数据框粘贴为 HTML table 的解决方案。这是使用 xtable
包。部分解决方案归功于@lukeA 从这里 - How to show an excel worksheet in outlook body by R
下面是解决方法。
library(RDCOMClient)
library(xtable)
x <- head(mtcars)
y <- print(xtable(x), type="html", print.results=FALSE)
body <- paste0("<html>", y, "</html>")
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "TEST EMAIL"
outMail[["HTMLbody"]] = body
outMail$Send()
这是 Microsoft Outlook 中的输出结果。
我有一个数据框,如下所示。然后我将数据帧转换为 html table.
# Use RDCOMClient to send email from outlook
library(RDCOMClient)
# Use xtable to convert dataframe into html table
library(xtable)
# Create dataframe
df <- as.data.frame(mtcars[1:3,1:3])
# Create HTML object
df_html <- xtable(df)
现在,我正在使用电子邮件线程中提供的绝妙解决方案从 outlook 发送电子邮件 Sending email in R via outlook
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "dest@dest.com"
outMail[["subject"]] = "some subject"
outMail[["body"]] = df_html
## send it
outMail$Send()
对于我的电子邮件正文,我想要作为 html table 附加的数据框 df。当我执行上面的代码时,我收到以下错误消息。
Error in `[[<-`(`*tmp*`, "body", value = list(mpg = c(21, 21, 22.8), cyl = c(6, :
Can't attach the RDCOMServer package needed to create a generic COM object
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘RDCOMServer’
当我将行 outMail[["body"]] = df_html
更改为 outMail[["body"]] = paste0(df_html)
时,我收到了电子邮件,但输出不是 table。它在我的展望中如下所示。
c(21, 21, 22.8), c(6, 6, 4), c(160, 160, 108)
我希望这是 table。我怎样才能做到这一点?谢谢!
我终于找到了在 Microsoft Outlook 中将数据框粘贴为 HTML table 的解决方案。这是使用 xtable
包。部分解决方案归功于@lukeA 从这里 - How to show an excel worksheet in outlook body by R
下面是解决方法。
library(RDCOMClient)
library(xtable)
x <- head(mtcars)
y <- print(xtable(x), type="html", print.results=FALSE)
body <- paste0("<html>", y, "</html>")
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "TEST EMAIL"
outMail[["HTMLbody"]] = body
outMail$Send()
这是 Microsoft Outlook 中的输出结果。