Categories
Research Technology (ResTech)
September 26, 2017
Tim Bock shares step-by-step instructions on how to use R to automatically write and send emails based on automatically computed analyses.
This post explains how to use R to automatically write and send emails based on automatically computed analyses (yep, everything automated). This means that when analysis changes or is updated, the email body text changes as well. The email can then be automatically sent to clients based on a trigger event (e.g., only when results are interesting) or periodically. All of this is can be done by using R code in Displayr, as illustrated in this post.
To automatically write and send email reports we need to have three tools:
The first step is to perform the analysis. Presumably, you want to set this up so that you have a live feed of data with the analysis updating automatically. See Automatically Updating Data and Dashboards in Displayr for more about this.
Go to the Sendgrid account signup page to create your account. Enter a username, password and email address and select the 30-day free trial option which allows you to send up to 40,000 emails. Click the Create Account button and then complete the additional fields on the next page (your role, company size, etc.) to complete the account setup process.
Once your SendGrid account has been set up, you can now create a SendGrid API key. The API key is a code that will enable your own R code to ask SendGrid to send emails. From your SendGrid account dashboard:
The next step to automatically write and email reports is to write the email body. The message text for the email needs a string, stored in Displayr in an R Output. To create an R output, open Displayr and select Insert > Analysis > R Output. You then type in completely standard R code, referring to the data in your Displayr document.
In the rest of this post, I assume that the R Output that contains the email content is called body.
For example, a simple document may contain a set of NPS scores for different brands, like those shown in the table below.
Using R, I can pull out the highest and lowest results from the table (which in this case are for Google and IBM), and I can construct the following rather messy-looking piece of text:
If you sign in to Displayr you can look at the example code that I used to build this message from the table above.
It may look messy now, but when the email is created, the \n tags will be converted into line breaks, and it will produce an email that looks like this:
Additionally, you can include the URL of your published Displayr document in the message. To obtain the URL, first, export your document using Export > Web Page, and then copy and paste the URL of the resulting page.
Next, to generate the automated email, you will need to create an R Output in Displayr.
Add the following to the R CODE box which loads the R libraries needed to create and send the email.
1
2
3
|
require (httr) require (jsonlite) require (flipTime) |
The next line will determine how frequently to run the code and therefore send the email. This line ensures that the code is re-run every 24 hours and that the dashboard is automatically updated to show the latest results.
1
|
UpdateEvery (24, "hours" , options = "snapshot" ) |
Add the next line with your SendGrid the API Key entered in the quotes.
1
|
key1 = "your_api_key" #enter your API Key here |
Next, the email parameters are specified and the email message is constructed. To do this, enter the recipient’s email address in the first line (to.email). It is a good idea to first enter your own email address here so you can test if the script is working as expected. The sender’s email address is entered in the second line and can be any email address you choose (from.email). You can also specify the email subject and message body here. In the previous step, I described how to automatically write the content of the body. Alternatively, you could replace body with a static message in the fourth line of code below (e.g., message.body= “Hello here.”).
1
2
3
4
5
6
7
8
9
10
11
|
to.email = "[email protected]" from.email = "[email protected]" subject = "Testing Sendgrid on Displayr" message.body = body msg = sprintf ('{\"personalizations\": [{\"to\": [{\"email\": \"%s\"}]}], \"from\": {\"email\": \"%s\"}, \ "subject\": \"%s" , \"content\": [{\"type\": \"text/plain\", \"value\": \"%s\"}]}', to.email, from.email, subject, message.body) |
The final bit of code is the actual SendGrid API call which sends the email.
Note, if you want to limit emails sends only to a specific trigger event, it is as simple as adding a plain-old if statement before this code.
1
2
3
4
5
|
body = msg, config = add_headers ( "Authorization" = sprintf ( "Bearer %s" , key1), "Content-Type" = "application/json" ), verbose ()) |
Check the Automatic check box next to the Calculate button to run the code. You should receive an email at the recipient email address you specified in the code.
You may want to apply some custom formatting to your emails rather than just sending them as plain text. To do this, you only need to change the content parameter line in the R code above from “text/plain” to “text/html”.
1
|
\"content\": [{\"type\": \"text/html\", |
You can now use standard HTML formatting tags in your message body text. For example, you can replace the body variable above with the following to generate an HTML-styled email.
body = paste(“<p>Dear Tim,</p>”,
“<p>Here are the latest results from your NPS survey:</p>”,
“<ul><li>The highest NPS score this month was <strong>”, max.value, “</strong> for <strong>”, max.label, “</strong>.</li>”,
“<li>The lowest NPS score this month was <strong>”, min.value, “</strong> for <strong>”, min.label,”</strong></a>.</ul>”,
“<p>Click <a href=’https://app.displayr.com/Dashboard?id=53c67902-3f3f-4c06-bcfd-d6c977d0ef99′>here</a> for the full report.</p>”,
“<p>Kind regards,<br />Tim</p>”)
The following email will be generated.
You can find more information on basic HTML styling options at https://www.w3schools.com/html/html_styles.asp.
Comments
Comments are moderated to ensure respect towards the author and to prevent spam or self-promotion. Your comment may be edited, rejected, or approved based on these criteria. By commenting, you accept these terms and take responsibility for your contributions.
Disclaimer
The views, opinions, data, and methodologies expressed above are those of the contributor(s) and do not necessarily reflect or represent the official policies, positions, or beliefs of Greenbook.
More from Tim Bock
Tim Bock on utilizing correspondence analysis.
Visualizations can summarize patterns that are commonly hidden in a simulator
Bad visuals stress the need for charts to be interpretable in seconds
Visualizing data can be made easier by utilizing small charts for comparison and analysis
Sign Up for
Updates
Get content that matters, written by top insights industry experts, delivered right to your inbox.
67k+ subscribers