Basic Builds :: R Markdown with Automated Email Delivery and CSV Attachment

Basic Builds :: R Markdown with Automated Email Delivery and CSV AttachmentKelly O'BriantBlockedUnblockFollowFollowingJan 6TL;DR: You can add a data file email attachment to R Markdown docs by defining it in rmd_output_metadataBasic Builds is a series of articles providing code templates for data products published to RStudio Connect.

Link to the full code templateGoal: 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 RExample 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_documentrmd_output_metadata: rsc_email_subject: "CSV Data Report" rsc_email_attachments: – "df.

csv"—Create random datadf <- data.

frame(a=rnorm(10), b=rnorm(10))Write data to a CSV filewrite.

csv(df, "df.

csv", row.

names=FALSE)Plot datalibrary(ggplot2)df_plot <- ggplot(data = df, aes(x = a, y = b)) + geom_point()Save the plot as a PNG image filelibrary(htmltools)ggplot2::ggsave( "plot.

png", plot = df_plot, device = "png", width = 5, height = 5, dpi = "screen")# Encode the PNG image as base64plot_base64 <- base64enc::base64encode("plot.

png")Create email body html and add imageslibrary(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 = "!."))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:Left: R Markdown Code, Right: Example EmailThis 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 Connect1.

Send a test emailAfter 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:Send an immediate test email to yourself or a viewer group2.

Set an output scheduleDefine how frequently the report should be run by navigating to the Schedule settings panel.

Refinement selections available depend on which schedule type you choose:Preview of all schedule type options in RStudio Connect3.

Set a recipients listFinally, 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.

Email recipient settings in RStudio ConnectYou’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.


. More details

Leave a Reply