Hi. I'm Sharon Machlis at IDG Communications,
here with Do More With R Episode 58: Email from R - including graphics!
How do you share your R work with others? R Markdown is one good way,
because it's easy to mix text narrative, calculation results,
and graphics. But how do you share your R Markdown documents with colleagues?
You can post them somewhere, email them as attachments . . .
Or, you can turn an R Markdown document into an
email message! . . . and send it in the body of your email, right from R
– ggplot graphs included! That’s thanks to the blastula package from RStudio.
Here's how it works.
First, not surprisingly, install and load blastula.
There are 2 ways to create an email with blastula.
My favorite is to start with an R Markdown document.
Use email as the output format, make sure to include a title, and you're ready to go. The other
is to use blastula’s compose_email() function, which is a bit more manual. I’ll use R Markdown.
I suggest creating an empty document.
Here’s what you want at the top: A title and the output format as email. Then create an
R Markdown document as usual. Note that HTML widgets won't work -- emails won’t
run JavaScript. But ggplot works fine. Here’s a sample document.
You can preview what it looks like the usual way, by clicking the knit button.
You’ll want to save this to a blastula email object with the package's render_email() function.
You can preview the email object with print().
So now it's time to send an email!
To do this, you need access to an SMTP server. Outlook, Gmail, and a lot of other email services
use SMTP. But to use blastula, you need to have access to send mail programmatically
through that server. Depending on security settings, you may not – especially at work.
If you're using personal gmail, you need to set your account to allow what Google considers
"less secure" apps to access your account.
I would not recommend doing this for a primary Google account that has sensitive information.
I've got a secondary Google account I use for a hobby local neighborhood events
site . . . which I haven't really used in over a year now, thanks to the pandemic.
So that's a good one for a demo! You need to go to Manage your Google Account > Security > and
then scroll down to where it says "Less secure app access". You'll be warned
not to do this (and with good reason, I'm turning it back off after this demo)
I can save my gmail user name and gmail server settings with blastula's
create_smtp_creds_key() function. This saves your user name and provider server settings
but not your password for better security; you need to enter your password with this command.
I already ran this for my account. When I did, I got this message
It’s telling me that when I’m ready to send, I should use credentials = creds_key(“gmail”).
OK time to test it out! Here’s the format for the smtp_send() function:
Start with the email object, include from, to, a subject, and then credentials. That’s
it! Let me run the code and go to my do more with r mailbox
And there it is!
An easy way to share your R analysis with others.
That’s it for this episode, thanks for watching! For more R tips, head to the
Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R
You can also find the Do More With R playlist on YouTube’s
IDG Tech Talk channel -- where you can subscribe so you never
miss an episode. Hope to see you next time. Stay healthy and safe, everyone!