如何发送电子邮件的R降价报告? [英] How to send R markdown report in body of email?

查看:530
本文介绍了如何发送电子邮件的R降价报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:Brandon Bertelsen的回答:






布兰登的回答产生以下输出。
它不会生成漂亮的表格或突出显示代码,如Rstudio,它崩溃在一些html文件与unicode,所以我没有使用它来自动化我的电子邮件报告。



我目前的方法是使用Rstudio编译为html,打开HTML中的html文档,然后将html文档复制并粘贴到gmail中。这很好,看到这个要点:



原始问题:






有没有一种简单的方式发送一个R markdown文档作为电子邮件的正文,以便电子邮件的正文看起来相似使用Rstudio的针织HTML的结果?



这是一个基本的可重复的示例,使用 knitr rmarkdown ,和 mailR



example.Rmd



  --- 
标题:电子邮件报告
输出:
html_document:
self_contained:no
---

```{r}
摘要(汽车)
```

你还可以嵌入地块,例如:

```{r,echo = FALSE}
plot(cars)
```

我使用 self_contained:no ,因为默认base64编码不适用于 mailR ( Yihui在






,rmarkdown电子邮件看起来像这样。 (请注意,它还包括一堆JavaScript文件 - 我想我必须写一些脚本来删除它们)











但是他们都不像报告是由Rstudio的Knit as HTML生成的,如下所示:





任何建议?



我认为一个真正的修复可能涉及到html文件的一些后处理,它将电子邮件方式的CSS样式整合到一起,同时删除了javascript文件。



现在,我将使用 knitr 包。



如果有什么不清楚,请让我知道,我会改进这个问题。



相关SO帖子:



在R中有什么办法发送RMarkdown v2 html文件作为邮件的正文



解决方案主要的问题是电子邮件阅读器剥离您的代码,不允许外部导入。要获得基本的CSS支持,最好的策略是使用内联样式来保持一致的视图。我们会在一分钟内回覆那个。



首先,您必须设置一些不同的Rmd文档,以便排除所有额外的JavaScript文件。 主题突出显示 mathjax 应该都是。请注意,我添加了一个 css 属性。

  --- 
标题:电子邮件报告
输出:
html_document:
self_contained:no
主题:null
高亮显示:null
mathjax: null
css:ink.css
---

```{r}
摘要(汽车)
```

您还可以嵌入图,例如:

```{r,echo = FALSE}
plot(cars)
```

ink.css 来自http://foundation.zurb.com/emails 。我建议使用它作为您的基础主题。



有许多不同的脚本可以用来内联你的css(这是一个动词),我已经包括这里的说明是使用 premailer 一个python包。不幸的是,他们都不会支持非常复杂的CSS,如bootstrap。所以你只需要用自己的风格,使用墨水或任何作为你的基础。



你可能需要安装一些元素,对我来说在Ubuntu :

  sudo apt-get install python-pip libxslt1-dev 
sudo pip install premailer

现在,你可以这样做。

  library(rmarkdown)
库(mailR)
rmarkdown :: render(example.Rmd )
system(python -m premailer -f example.html -o output.html)


send.mail(
from =me @ gmail .com
to =me@gmail.com,
subject =R Markdown Report - rmarkdown,
html = T,
inline = T,
body =output.html,
smtp = list(
host.name =smtp.gmail.com,
port = 465,
user.name = 我,
passwd =密码,
ssl = T),
authenticate = T,
send = T)

免责声明:您的里程可能会有所不同,具体取决于您的目标是哪个电子邮件阅读器。


update: Brandon Bertelsen's answer:


Brandon's answer produces the following output. It doesn't produce nice tables or highlight code like Rstudio does, and it crashes on some html files with unicode, so I'm not using it to automate my email reports.

My current approach is to compile with Rstudio to html, open the html document in chrome, and then copy and paste the html document into gmail. This works pretty well, see this gist: https://gist.github.com/nelsonauner/a68b5a808c232ce7817e

original question:


Is there an easy way to send an R markdown document as the body of an email, so that the body of the email looks similar to the results of using Rstudio's "Knit HTML" ?

Here's a basic reproducible example using knitr, rmarkdown, and 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)
```

I'm using self_contained: no since the default base64 encoding does not work with mailR (recommended by Yihui in 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)

Both emails send successfully.

The knitted email looks like this:



and the rmarkdown email looks like this. (Notice that it also includes a bunch of javascript files--I think I'd have to write some scripts to remove them)



But neither of them look as nice as the report that is produced from Rstudio's "Knit as HTML", which looks like this:

Any suggestions?

I think a true fix might involve some postprocessing of the html file that incorporate the css styling in an email-friendly way while removing the javascript files.

For now, I'll use the knitr package.

Please let me know if something isn't clear and I'll improve the question.

Relevant SO posts:

In R is there any way to send an RMarkdown v2 html file as the body of an email

mailR: how to send rmarkdown documents as body in email?

解决方案

The main problem is that email readers strip your code and don't allow external imports. To get basic CSS support, the best strategy is to use inline styles to have a consistent view. We'll circle back to that in a minute.

First, you have to setup your Rmd document a little differently so it excludes all the extra javascript files. theme, highlight and mathjax should all be null. Notice, I've added a css attribute.

---
title: "Report for email"
output: 
  html_document: 
    self_contained: no
    theme: null
    highlight: null
    mathjax: null
    css: ink.css
---

```{r}
summary(cars)  
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

ink.css comes from http://foundation.zurb.com/emails. I recommend using this as your base theme.

There are a number of different scripts you can use to "inline" your css (that's a verb), I've included instructions here for using premailer a python package. Unfortunately, none of them will support very complicated CSS like bootstrap. So you'll just have to make do with your own style built up using ink or whatever as your foundation.

You may need to install some elements, for me on Ubuntu:

sudo apt-get install python-pip libxslt1-dev
sudo pip install premailer

Now, you can do something like this.

library(rmarkdown)
library(mailR)
rmarkdown::render("example.Rmd")
system("python -m premailer -f example.html -o output.html")


send.mail(
  from = "me@gmail.com",
  to = "me@gmail.com",
  subject = "R Markdown Report - rmarkdown",
  html = T,
  inline = T,
  body = "output.html",
  smtp = list(
     host.name = "smtp.gmail.com", 
     port = 465, 
     user.name = "me",    
     passwd = "password", 
     ssl = T),
  authenticate = T,
  send = T)

DISCLAIMER: Your mileage may vary wildly depending on which email reader is your target

这篇关于如何发送电子邮件的R降价报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆