Emails from R: Blastula 0.3

[This article was first published on RStudio Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We’re pleased to announce blastula, a package for creating beautiful custom
emails in R. At RStudio, we love interactive dashboards, but some situations
call for a different communication mechanism. Use blastula to:

  • Compose custom email bodies based on code, code output, and markdown
  • Send emails using SMTP servers – even GMail – or integrate with production services like RStudio Connect

Blastula makes it easy to send notifications for everything from anomaly detection to fantasy basketball results, all without leaving R.

To get started, install blastula from CRAN:

install.packages('blastula')

Creating Email

Blastula’s unique strength is creating custom HTML email bodies that render in a
variety of email clients, including mobile. The recommended way to create email
is using blastula’s R Markdown output format. blastula::blastula_email. The
body of the email will respect the R Markdown output, including markdown syntax
and code chunk outputs.

---
output: blastula::blastula_email 
--- 

Hi Team,

This *important* forecast needs to go out today.

```{r echo=FALSE}
model <- arima(presidents, c(1, 0, 0))
predict(model, 3)
```

To create the email from the R Markdown document, use render_email:

email <- render_email('email.Rmd')

The resulting email object can be previewed in RStudio.

Email from R Markdown

Alternatively, it is possible to create an email without R Markdown, by using
the compose_email function to combine text, images, and even plots:

library(blastula)
library(ggplot2)
library(glue)
plot <- qplot(disp, hp, data = mtcars, colour = mpg)
plot_email <- add_ggplot(plot)

email <- compose_email(
  body = md(c(
     "Team, How would you plot the relationship between these 3 variables?",
      plot_email
  ))
)

Preview blastula emails in RStudio

Visit the documentation to learn how to embed
images, set email headers and footers, and even add call-to-action buttons.

Sending Custom Emails with SMTP

To send email, blastula includes functions to access SMTP servers such as GMail, Outlook, and Office365.

First, securely tell blastula about your SMTP server:

create_smtp_creds_key(
  id = "gmail",
  user = "[email protected]",
  provider = "gmail"
)

Next, use the SMTP service to send your custom email:

email %>%
  smtp_send(
    from = "[email protected]",
    to = "[email protected]",
    subject = "Testing the `smtp_send()` function",
    credentials = creds_key(id = "gmail")
  )

Sending Custom Emails with RStudio Connect

Organizations can use blastula in production on RStudio Connect. For instance, we
use blastula to track critical services like our support ticket
volume and our staff training schedules.

An easy way to get started is to access the RStudio Connect examples:

blastula::prepare_rsc_example_files()

Publish the resulting R Markdown document to RStudio Connect, where it can be
scheduled for regular
execution

and distributed to stakeholders.

Schedule and Email in RStudio Connect

Blastula offers three additional functions to make it easier to create emails
for RStudio Connect.

  • render_connect_email automatically adds a footer to the email with useful links back to the content on RStudio Connect.
  • attach_connect_email ensures RStudio Connect sends the custom email, and also makes it easy to customize the subject line, include additional email attachments, and optionally attach the report output.
  • suppress_scheduled_email() allows you to skip sending the email. This pattern is very powerful. For example, reports can be run once a day, but only distributed if certain conditions are met.

Together, these three functions can be used to send proactive notifcations:

if (demand_forecast > 1000) {
  render_connect_email(input = "alert-supply-team-email.Rmd") %>%
  attach_connect_email(
    subject = sprintf("We need to prepare %d units!", demand_forecast),
    attach_output = TRUE,
    attachments = c("demand_forecast_data.csv")
  )
} else {
  suppress_scheduled_email() 
}

Please be sure to visit the blastula
website
to find additional
resources. Afterall, who doesn’t want a ggplot in their inbox?

To leave a comment for the author, please follow the link and comment on their blog: RStudio Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)