When monitoring a data source, model, or other automated process, it’s convienent to have method for easily delivering performance metrics and notifying you whenever something is amiss. One option is to use a dashboard; however, this requires active time and effort to grab numbers and catch errors. An alternative approach is to send an email alert on the performance of the process. In this exercise set, we will explore the email approach using the
Exercises in this section will be solved using the
mailR package as well as basic HTML and CSS. It is recommended to take a look at the
mailR documentation before continuing.
Answers to the exercises are available here.
Let’s begin by sending “Hello World. This is my email!” as the
body parameter from yourself to yourself.
By passing in a vector for the
to parameter, you can send the email to multiple recipients. Send the above email to yourself and a friend.
So far, your emails have had no subject. Send the email from Exercise 1 to yourself with “Email Testing” for the
With this package, we can take full advantage of CSS when constructing the body of an email. Send the email from the previous exercise from yourself to yourself where “Hello World.” is now
red and “This is my email!” is now
Note: make sure that
html = TRUE.
If you write a complex email containing images, dynamic elements, etc. as an HTML file, then you can reference this file with the
body parameter. Create an HTML file containing “Hello World. This is my email!” called my_email.html. Send this email to yourself.
knitr, you can compile HTML files. Compile the default
knitr document that uses the
mtcars dataset to an HTML file and email this to yourself.
Create a new R script called mailr_six.R containing your code from the above exercises and attach that to your email by referencing the file path to mailr_six.R in the
attach.files parameter. Send this email from yourself to yourself.
The attached R script above does not have a description or a name. Add these in the
file.names parameters, respectively. Send the resulting email to yourself.
Just as with the recicipents, you can attach multiple files, descriptions, and names by passing in vectors to the respective parameters. Create a new R script called mailr_eight.R containing your code from the above exercises and attach both mailr_six.R and mailr_eight.R to your email. Send the resulting email to yourself.
Create a new R script where a random integer called important_number is generated. If important_number is even, then send an email to yourself notifying you that important_number is even.