【发布时间】:2016-03-17 18:30:02
【问题描述】:
更新:Brandon Bertelsen 的回答:
Brandon 的回答产生以下输出。 它不会像 Rstudio 那样生成漂亮的表格或突出显示代码,并且它会在一些带有 unicode 的 html 文件上崩溃,所以我没有使用它来自动化我的电子邮件报告。
我目前的做法是用Rstudio编译成html,用chrome打开html文档,然后将html文档复制粘贴到gmail中。这很好用,请参阅此要点:https://gist.github.com/nelsonauner/a68b5a808c232ce7817e
原问题:
是否有一种简单的方法可以将 R markdown 文档作为电子邮件正文发送,以使电子邮件正文看起来类似于使用 Rstudio 的“Knit HTML”的结果?
这是一个使用 knitr、rmarkdown 和 mailR 的基本可重现示例
example.Rmd
---
title: "Report for email"
output:
html_document:
self_contained: no
---
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
我正在使用self_contained: no,因为默认的base64编码不适用于mailR(由Yihui在this SO post推荐)
knit_and_send.R
# compile using rmarkdown
library(rmarkdown)
rmarkdown::render("example.Rmd")
library(mailR)
send.mail(from = "me@gmail.com",
to = "me@gmail.com",
subject = "R Markdown Report - rmarkdown",
html = T,
inline = T,
body = "example.html",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "me", passwd = "password", ssl = T),
authenticate = T,
send = T)
#compile using knitr
library(knitr)
knit2html("example.Rmd",options="")
send.mail(from = "me@gmail.com",
to = "me@gmail.com",
subject = "R Markdown Report - knitr",
html = T,
inline = T,
body = "example.html",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "me", passwd = "password", ssl = T),
authenticate = T,
send = T)
两封邮件都发送成功。
编织的电子邮件如下所示:
并且 rmarkdown 电子邮件看起来像这样。 (请注意,它还包括一堆 javascript 文件——我想我必须编写一些脚本来删除它们)
但是它们看起来都没有 Rstudio 的“Knit as HTML”生成的报告那么好,看起来像这样:
有什么建议吗?
我认为真正的修复可能涉及对 html 文件进行一些后处理,以电子邮件友好的方式合并 css 样式,同时删除 javascript 文件。
现在,我将使用knitr 包。
如果有什么不清楚的地方请告诉我,我会改进这个问题。
相关 SO 帖子:
In R is there any way to send an RMarkdown v2 html file as the body of an email
【问题讨论】:
-
看起来我们都在寻找如何做到这一点但没有成功......你成功了吗?
-
不!我目前的做法是在 Rstudio 中编译为 markdown,打开输出的 html 文件,crtl-C crtl-P 到电子邮件中,然后发送:( Noam ross 在这里建议一个包:twitter.com/noamross/status/676760045187694596。仍然希望得到@987654330 的关注@
-
好的,我发布了您帖子的另一个扩展,用于交互式图表...您的复制粘贴甚至无法正常工作。谢谢
-
@statquant 你能分享问题的链接吗?您无法通过电子邮件发送交互式图表(因为电子邮件是静态的)所以问题不仅仅是复制/粘贴
标签: r email knitr r-markdown