Customized Conditional Emails with RStudio Connect

This article uses the example of an Etsy shop to demonstrate an R Markdown-based conditional custom email workflow using RStudio Connect.

Scenario: Every day new orders flow in and inventory is depleted. Our goal is to create an R Markdown report that tracks basic statistics for orders, sales and inventory levels, while also sending alerts when inventory gets low.

Goal: Receive an automated alert email whenever inventory drops below a warning threshold

The Etsy shop sells one style of cookie cutter (R logo shaped) in three color-ways: Purple, Black, and Red. They are printed in batches of nine, and each batch takes about five hours to create. If inventory is getting low for a specific color, we want to receive an alert so the 3D printer can start creating a new batch of cookie cutters overnight.

Solution Components #

Every time the report runs, it evaluates the orders and inventory data, then determines whether or not to trigger a custom email event. To achieve this behavior, a code chunk is added to the bottom of the main report which contains logic to either attach or suppress an email based on the value of low_inventory.

low_inventory is a boolean that gets set to TRUE when the inventory count for any individual color drops into the alert threshold (<= 2).

The functions render_connect_email, attach_connect_email, and suppress_scheduled_email are all available in the blastula package.

The render_connect_email function references an additional file, inventory-alert.Rmd. This is where the email body contents are defined:

To define an email message, the YAML header output needs to be of type blastula::blastula_email. The rest can be crafted like a regular R Markdown document. This example includes a banner image, some plain text, a markdown header, and an unordered list of items that include inline R code defined in the original (parent) report.

The option to send or suppress is useful, especially when you don’t want to flood someone’s inbox with repetitive alerts. But now that blastula provides a function to render an email from a separate R Markdown document, it follows that we may also want to define conditional logic for attaching either/or emails.

Either/Or Conditional Custom Emails #

In this case, I still want to get an alert email if my shop inventory is low; but if inventory is stable, I want to receive a general update on my basic shop statistics.

The code chunk in the main report is updated as follows:

Now, instead of suppressing email in the else block, an additional render function points to a different email document: inventory-update.Rmd.

The Orders and Inventory Update email has a different banner image and includes running shop totals for orders and units sold which were tabulated in the main Cookie Cutter Shop Report document.

It also contains inventory burndown charts for each of the three color-ways. These charts are the output of a code chunk from the main report called orders_inventory. By including an empty code chunk in the email document, also labeled orders_inventory, that section of the original report gets reused.

Final Either/Or Conditional Solution #

The final R Markdown document deployed to RStudio Connect sends one of the two emails based on the outcome of the conditional test for low_inventory: