ŷhat

Building email reports with R

by yhat

Communicating your results

Most of the time just writing a bunch of code to analyze data isn't enough, you need to be able to communicate your results to the rest of your company. This process can take many forms, but we're going to focus on a tried and true method--the automated email. I'll just say upfront that it's your job not to get too carried away. Nobody wants their inbox completely stuffed with your ggplots (no matter how pretty they might be).

A brief aside

If you are someone at an organization that is drowing in automated email, check out eventifier. It's a simple push API with node.js that comes with a built in Chrome Extension. GLG uses it as an alternative to receiving an email for event driven workflows. They've got some other really cool open source projects that deserve way more love (check out dbSockets), but what were we talking about? Oh right, email!

The package

The package we're going to be using is (shockingly) called sendmailR. You can read more about how to use it here. It's relatively new and I couldn't find any good tutorials or blog posts about it, so I figured we'd give it a go here. Go ahead and install it via CRAN:

install.packages("sendmailR")
library(sendmailR)
?sendmail

Credentials

Specifying your credentials is always the hard part. The docs never seem to give the right server configuration. sendmailR keeps things pretty simple. My email is setup through gmail, so the first thing I need to do is tell sendmailR which server to point to.

sendmail_options(smtpServer="ASPMX.L.GOOGLE.COM")

Your first email

Ok! If you've read this far then the pain and suffering is over! Let's actually send an email.

# gmail requires the angle bracket syntax for from/to (i.e. <myemail@myaddress.com>)
from <- "<you@me.com>"
to <- "<you@me.com>"
subject <- "Welcome!"
msg <- "To The Rock"
sendmail(from, to, subject, msg)

And if you're following along then you should've just received your first email from an R script. Shaboom!

Sending plots

Alright let's cut to the chase. Anyone can automate an email with some text in it, but what you really want to do is send some graphics that will blow your colleagues away!

sendmailR makes this pretty easy to do. They've built a mime_part function that can handle pretty much any data strcuture in R (including plots) and convert it to email-sendable format.

library(ggplot2)

data <- data.frame(day=seq(as.Date("2013-01-01"), as.Date("2013-11-01"), 1))
data$value <- cumsum(sample(c(-1, 1), nrow(data), replace=T))
p <- ggplot(aes(x=day, y=value), data=data) +
  geom_line() + 
  stat_smooth(se=F) +
  ggtitle("Random Walk") +
  xlab("Time") +
  ylab("Y")

body <- list(
  mime_part(p, "GregsReport")
)
subject <- "An Important Report"
sendmail(from, to, subject, body)

Adding text and tables

Customizing the body of the email is pretty easy to do. Just make any text you want to include the first argument to your body and don't give it a mime type. mime_part also accepts data frames, so you can attach accompanying data that someone might want to analyze on their own.

body <- c(
  "Greg -- This is your report for the day. Have a great Friday!",
  mime_part(p, "GregsReport"),
  mime_part(data, "GregsData")
)
sendmail(from, to, "Friday Corporate Numbers", body)

Giving your reports some flair

The only thing wrong with my email is it's a little boring. I'll probably get tired of looking at the same old ggplots every Friday. What might spice things up a little?

msg <- "
  <html>
    <body>
      <h1>YOU'RE THE MAN NOW DOG!</h1>
      <img src=\"http://gifs.jessfraz.com/images/sean-connery-ytmnd.jpg\">
    </body>
  </html>"
sendmail(from, to, subject, msg,
    headers=list("Content-type"="text/html", "charset"="iso-8859-1"))

Before you go

In doing this post I found an R package with a lot of promise: EasyHTMLReport. Unfortunately I couldn't get it working with gmail. It's still a relatively new package, so just keep your eye on it for future reference.

Interested in ŷhat? Learn More