我有一个数据,如下所示。然后将dataframe转换为html表。
# 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)
现在,我使用电子邮件线程Sending email in R via outlook中给出的极好的解决方案发送了一封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()
对于我的电子邮件的正文,我想要数据框架df,我想附加作为一个html表。当我执行上述代码时,我会得到以下错误消息。
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)
时,我会收到电子邮件,但输出不会作为表出现。我的展望如下所示。
c(21, 21, 22.8), c(6, 6, 4), c(160, 160, 108)
我想要这张桌子。我怎样才能做到这一点?谢谢!
发布于 2017-11-13 13:21:40
最后,我找到了一种解决方案,将dataframe粘贴为Microsoft中的HTML表。这是使用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中输出的样子。
https://stackoverflow.com/questions/46978381
复制相似问题