Closed Captioning Closed captioning available on our YouTube channel

How to send email with R and Gmail

InfoWorld | Jun 5, 2019

See how to automate sending email with the R programming language and Gmail.

Copyright © 2019 IDG Communications, Inc.

Hi. I’m Sharon Machlis at IDG Communications, here with episode 28 of Do More With R: Send email from your R script with Gmail.
At some point in your R life, you’re likely to want to share results of your analysis with non-R-using colleagues. There are lots of ways to do this. One of the easiest (and cheapest) is emailing your results.
But it’s kind of sad to automate your whole analysis workflow, and then have to manually send an email. Fortunately, you don’t have to. There are several R packages that let you send email directly from an R script. In this episode, I’ll demo one of them: gmailr by Jim Hester, who’s now a software engineer at RStudio.
Obviously, you’ll need a Gmail account, which is free to set up if you don’t have one. Then, before you can use that account, you’ll need to set it up for API access. Here’s how.
Go to console dot developers dot google dot com. Yes, that’s a sub-sub domain:
At the top you should see a choice to “Enable APIs and Services.” Click that.
Next you’ll want to search for the gmail API. Click on that and then click Enable. If you don’t have any projects already in your account, you may need to add one. I already have one called Do More With R.
We need credentials, so I’ll click Create Credentials at the top right. According to Jim Hester’s instructions, we need a client ID, so I’ll choose OAuth Client ID.
Now it’s asking for an application type. Since “R script” isn’t here, I want to choose “Other.” But all the radio buttons are greyed out. Don’t worry, that’s because we haven’t configured the consent screen. That’s easy to miss – see it here at the top right. So click on that.
Your email address should be in the form already. The only other requirement is a name for the application. Not surprisingly, I’ll say Do More With R.
Jim says the rest of the defaults are fine, so I’ll scroll down and save. Now I can pick Application type Other, give it a name, and click Create.
You’ll be given your client ID and client secret. (If you’re wondering why I’m letting you see these – a very bad security practice – that’s because I’ll be deleting these credentials before you see this video. )
You can use your client ID and secret by adding them to your R environment if you want. But Jim suggests downloading the json file instead. I’ll download it into my working directory and call it, yes, DoMoreWithR.json.
OK, setup on the Google side is done! Now time for some R code.
First, make sure you’ve got the gmailr package installed, it’s available on CRAN. Load the library. Before doing anything else, you want to set up your working session to use your downloaded json credentials file. Do that with the use_secret_file() function, and the name of your file as the argument.
Actually sending a message is the easy part!
Here I’ve downloaded monthly US unemployment rates, and then created a text string called latest_msg with info about the latest unemployment rate. For a plain-text email message, I want to create a “mime” object, and then add a to address, a from address, subject text, and my message body.
If I check the structure of my_text_message you can see it’s a list, and the class is mime.
For this test, I’ll send a message to myself at
After creating a mime message object, I can send it with the send_message() function. The argument is just the name of my mime object, in this case my_email_message.
When you run send_message() the first time, you’ll likely be asked if you want to cache authorization between R sessions. I suggest you say yes. Since I said yes earlier, I won’t be asked to sign into my Google account again. The first time you try this, you’ll probably be asked in your browser to authorize your R script to use your Google account.
There’s more you can do with this. One option is to create an HTML message, so you can use markup like bold and italic.
Here my message body includes HTML like paragraph marks and bold and italic. I’ll send that to an external email account,
Unfortunately, I don’t know a way to easily include an image generated from R directly in the message body. But it’s pretty straightforward to include one as an attachment.
At the top of this script, I’m turning my unemployment rate data into a data frame with metrics from 2000 and later, so I can use ggplot to graph it, and then save the graph to a file.
This next part is what’s important to know for email. First, like before, I’m creating a text string for my message text with the glue package. You can use paste or paste0, too, but I like glue. What’s new is the last two lines of code creating my mime object. That last line, attach_file(), attaches my png file to the email. The line before is important if you want text to show up in the body of the email. Without using both text_body() and attach_part() for the body text, it won’t show up when you attach a file. Just something to remember.
Now I can send it as usual using send_message If you want, you can use the function create_draft() instead of send_message() if you’d like to create a draft message in your gmail account. then look for the draft>.
There you have it: Email from an R script. That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at go dot infoworld dot com slash more with R, all lowercase except for the R. You can also find the Do More With R playlist on the YouTube IDG Tech Talk channel. Hope to see you next episode!
Featured videos from