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.
布兰登的回答产生了以下输出。它不像Rstudio那样生成漂亮的表或突出显示代码,而且它会在一些使用unicode的html文件上崩溃,所以我不会使用它来自动生成电子邮件报告。
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
我目前的方法是使用Rstudio编译html,在chrome中打开html文档,然后将html文档复制粘贴到gmail中。这非常有效,请参阅要点: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" ?
是否有一种简单的方法可以将一个R markdown文档作为电子邮件的主体发送,从而使电子邮件的主体看起来与使用Rstudio的“编织HTML”的结果相似?
Here's a basic reproducible example using knitr
, rmarkdown
, and mailR
这里有一个使用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)
```
I'm using self_contained: no
since the default base64 encoding does not work with mailR
(recommended by Yihui in this SO post)
我使用了self_contains: no,因为默认的base64编码不能使用mailR(由Yihui在本文中推荐)
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)
rmarkdown的电子邮件是这样的。(请注意,它还包含一些javascript文件——我想我必须编写一些脚本来删除它们)
But neither of them look as nice as the report that is produced from Rstudio's "Knit as HTML", which looks like this:
但它们都没有Rstudio的“针织如HTML”(针织如HTML)报告那么好看,报告是这样的:
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.
我认为一个真正的修复可能需要对html文件进行一些后处理,在删除javascript文件的同时,以一种友好的方式合并css样式。
For now, I'll use the knitr
package.
现在,我将使用knitr包。
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
在R中有任何方法可以将RMarkdown v2 html文件作为电子邮件的主体发送
mailR: how to send rmarkdown documents as body in email?
邮件:如何以正文的形式发送邮件?
1 个解决方案
#1
12
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.
主要的问题是电子邮件阅读器剥夺了您的代码并且不允许外部导入。要获得基本的CSS支持,最好的策略是使用内联样式以获得一致的视图。我们一会再回到这个问题上。
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.
首先,您必须以稍微不同的方式设置Rmd文档,以便它排除所有额外的javascript文件。主题、突出显示和mathjax都应该为空。注意,我添加了一个css属性。
---
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.
墨水。css来自http://foundation.zurb.com/emails。我建议您使用这个作为基本主题。
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.
有许多不同的脚本可以用来“内联”您的css(那是一个动词),我在这里包含了使用python包premailer的说明。不幸的是,它们都不支持非常复杂的CSS,比如bootstrap。所以你只需要使用你自己的风格,用墨水或者其他东西作为你的基础。
You may need to install some elements, for me on Ubuntu:
你可能需要在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
免责声明:你的里程可能会有很大的变化,这取决于你的目标读者是谁。
#1
12
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.
主要的问题是电子邮件阅读器剥夺了您的代码并且不允许外部导入。要获得基本的CSS支持,最好的策略是使用内联样式以获得一致的视图。我们一会再回到这个问题上。
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.
首先,您必须以稍微不同的方式设置Rmd文档,以便它排除所有额外的javascript文件。主题、突出显示和mathjax都应该为空。注意,我添加了一个css属性。
---
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.
墨水。css来自http://foundation.zurb.com/emails。我建议您使用这个作为基本主题。
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.
有许多不同的脚本可以用来“内联”您的css(那是一个动词),我在这里包含了使用python包premailer的说明。不幸的是,它们都不支持非常复杂的CSS,比如bootstrap。所以你只需要使用你自己的风格,用墨水或者其他东西作为你的基础。
You may need to install some elements, for me on Ubuntu:
你可能需要在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
免责声明:你的里程可能会有很大的变化,这取决于你的目标读者是谁。