Basic Builds :: R Markdown with Automated Email Delivery and CSV Attachment
TL;DR: You can add a data file email attachment to R Markdown docs by defining it in rmd_output_metadata
Basic Builds is a series of articles providing code templates for data products published to RStudio Connect.
Goal: Publish an R Markdown document to RStudio Connect which runs on a schedule and sends an email to defined recipients with each output update.
RStudio Connect is a platform for publishing many types of data products, including R Markdown documents. Embedding email templates into R Markdown is easy to do, and combined with the RStudio Connect scheduling feature, can be used to enable powerful, custom distribution of data analytics to stakeholders throughout your entire organization.
R Markdown emails are now highly customizable — this example is a template for how to send an email with a CSV data file attachment.
Use the template to create an R Markdown document in R
Example R Markdown with Email and CSV Attachment Template Outline:
- Define R Markdown output metadata in the YAML header
---
title: "Email Report and CSV File"
output: html_document
rmd_output_metadata:
rsc_email_subject: "CSV Data Report"
rsc_email_attachments:
- "df.csv"
---
- Create random data
df <- data.frame(a=rnorm(10), b=rnorm(10))
- Write data to a CSV file
write.csv(df, "df.csv", row.names=FALSE)
- Plot data
library(ggplot2)
df_plot <-
ggplot(data = df,
aes(x = a, y = b)) +
geom_point()
- Save the plot as a PNG image file
library(htmltools)ggplot2::ggsave(
"plot.png",
plot = df_plot,
device = "png",
width = 5,
height = 5,
dpi = "screen"
)
# Encode the PNG image as base64
plot_base64 <- base64enc::base64encode("plot.png")
- Create email body html and add images
library(glue)report_name <- Sys.getenv("RSC_REPORT_NAME")
report_url <- Sys.getenv("RSC_REPORT_URL")
message <- glue(paste(h1("data plot"),
p(img(src = "cid:plot.png")),
'--',
p('This {report_name} document is available at {report_url}'),
sep = "\n"))
images <- list(plot.png = plot_base64)
rmarkdown::output_metadata$set(rsc_email_body_html = message)
rmarkdown::output_metadata$set(rsc_email_images = images)
The template provided shows how to create email customization around subject line, attachment document, and message body:
This example R Markdown document generates new random data each time the report is run. Presumably, this would not be a common pattern or use case. Make this template useful by replacing the random data generation section with code that pulls data from a database, datastore or external API. Best practices for connecting to databases from R and RStudio professional products can be found here.
Publish the R Markdown document to RStudio Connect
1. Send a test email
After the document has been successfully published, send a test email to yourself at any time using the RStudio Connect content management tools. Click on the email icon as shown here:
2. Set an output schedule
Define how frequently the report should be run by navigating to the Schedule settings panel. Refinement selections available depend on which schedule type you choose:
3. Set a recipients list
Finally, define who should receive the email output by creating a recipients list.
- Recipients must have view access to the report. You may need to update the Access Settings before adding individual recipients.
- As the document owner, you will be placed on the email recipient list by default. Click the opt-out link to disable this default behavior.
You’re done! R Markdown email automation complete!
To learn more about RStudio Connect, you can: check out the User Guide, watch an introductory video, or schedule a demo with RStudio.