Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send R markdown report in body of email?

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

enter image description here

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 = "[email protected]",           to = "[email protected]",           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 = "[email protected]",           to = "[email protected]",           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:


knitted and emailed report


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)


enter image description here


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

enter image description here

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?

like image 315
rmstmppr Avatar asked Dec 11 '15 18:12

rmstmppr


People also ask

How do I share R Markdown in HTML?

By default, RMarkdown rendered as HTML are self-contained. This means you can compile the document into HTML file and simply email it to a colleague. It should have all that is needed to render the document on your colleague's web browser.

How do I embed an R code in markdown?

To insert a code chunk, press Ctrl + Alt + I in the source pane (top left pane in the default settings of RStudio). A code chunk will appear: Inside the code chunk you can write and run R-code.

How do I link text in R Markdown?

Hyperlinks are created using the syntax [text](link) , e.g., [RStudio](https://www.rstudio.com) . The syntax for images is similar: just add an exclamation mark, e.g., ![ alt text or image title](path/to/image) . Footnotes are put inside the square brackets after a caret ^[] , e.g., ^[This is a footnote.] .


1 Answers

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 = "[email protected]",   to = "[email protected]",   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

like image 79
Brandon Bertelsen Avatar answered Sep 21 '22 21:09

Brandon Bertelsen