Basic Builds: Conditional Custom Email Workflows on RStudio Connect

Kelly O'Briant
Jan 1, 2020 · 4 min read

A few years ago I opened a short-lived Etsy shop selling 3D printed R logo cookie cutters. I learned that Etsy shops are both thrilling and a big pain. There were always lots of things to keep track of — a project with lots of automation potential! I shut down the shop in 2017, but it’s still fun to think about how I could manage it better now.

R logo cookie cutters in action

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

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

Every day new orders flow in and inventory is depleted as I pack and ship. I’d like to produce an R Markdown report that tracks basic statistics for orders, sales and inventory levels.

The cookie cutter shop sells one style of 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, I’d like to receive an alert so I can start printing 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, I created a code chunk in 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.

You may note that the render_connect_email function references an additional file, inventory-alert.Rmd. This is where the email body contents are defined. The ability to craft custom emails in separate “child documents” is really nice — even for short, basic emails like this one:

---
title: "Action Required: Low Inventory Alert!"
output: blastula::blastula_email
---
<img src="low-inventory-banner.png">A low inventory alert has been triggered - Review the current inventory levels below to determine which filament colors need to be replenished today.### Current inventory by unit color:- **Purple:** `r inventory_levels$p_inventory`
- **Black:** `r inventory_levels$b_inventory`
- **Red:** `r inventory_levels$r_inventory`

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.

See the full code Gist: Conditional Suppression Example

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 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.

---
title: "Orders and Inventory Update"
output: blastula::blastula_email
---
<img src="cookie-shop-banner.png">## Orders and Inventory Update### Running Shop Totals- **Orders:** `r orders$total_orders`
- **Total units sold:** `r orders$purple + orders$black + orders$red`
### Remaining Inventory- **Purple filament:** `r inventory_levels$p_inventory`
- **Black filament:** `r inventory_levels$b_inventory`
- **Red filament:** `r inventory_levels$r_inventory`
```{r orders_inventory, echo=FALSE, message=FALSE, warning=FALSE}
```

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 Solution

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

I would have never tried to build a system like this using the old blastula custom email workflow on RStudio Connect. The 0.3.1 release of blastula has opened up a completely new set potential email integrations that I’m excited to explore.

If you build something cool with blastula and RStudio Connect, let us know! Reach out on the R Admins channel of RStudio Community.

RStudio Connect Digest

RStudio Connect is a publishing platform for the work data science teams create in R and Python

Kelly O'Briant

Written by

RStudio Connect Digest

RStudio Connect is a publishing platform for the work your teams create in R and Python. Share Shiny applications, R Markdown reports, Plumber APIs, dashboards, Jupyter Notebooks, interactive Python content, and more in one convenient place.

Kelly O'Briant

Written by

RStudio Connect Digest

RStudio Connect is a publishing platform for the work your teams create in R and Python. Share Shiny applications, R Markdown reports, Plumber APIs, dashboards, Jupyter Notebooks, interactive Python content, and more in one convenient place.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store