How do you share your R analyses with other folks? R Markdown is one very good way, simply because it’s straightforward to combine text narrative, calculation benefits, and graphics. But how do you share your R Markdown files with colleagues?
You may well article them somewhere, email them as attachments, or use RStudio Join (a industrial product or service) to share them. Or, you can turn an R Markdown document into an email concept, and send it in the human body of your email, proper from R – ggplot graphs involved. That’s many thanks to the blastula deal from RStudio.
Here’s how it will work.
Initial, not incredibly, install and load the deal with install.offers("blastula")
or controllers::install_github("rstudio/blastula")
.
Create an email concept with blastula
There are two ways to generate an email with blastula. My favored is to get started with an R Markdown document. Use blastula::blastula_email as the output structure, make guaranteed to involve a title, and you are all set to go. The other way is to use blastula’s compose_email()
operate, which necessitates additional manual coding for additional than a basic text email. For this demo, I’ll use R Markdown.
I recommend building an vacant document in RStudio by likely to File > New File > R Markdown and clicking on the Create Empty Document button.
For the YAML at the top, a document title and email output structure is needed, like this:
---
title: My Email Title
output: blastula::blastula_email
---
Then generate an R Markdown document as normal. Note that HTML widgets will not operate — the emails will not operate JavaScript. Nevertheless, ggplot will work wonderful, as in this sample document:
---
title: Useful graph!
output: blastula::blastula_email
--- Greetings all! I needed to present you this graph. If I had additional to say, I could use formatting like _italics_ and **bold**. HTML css will work, too, these as switching font colour. ```r echo = Fake
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
ggtitle("MPG by Fat")
```
You can preview what it looks like the normal way, by clicking the knit button in RStudio.
Next, preserve your .Rmd document to a blastula email item with blastula’s render_email()
operate, these as
library(blastula)
my_email_item <- render_email('blastula_test.Rmd')
You can preview the email item with print(my_email_item)
. If you look at the object’s class with class(my_email_item)
, you need to see
"blastula_concept" "email_concept"
Ship your email concept from R
Now it’s time to send the email.
To do this, you require access to an SMTP server. Outlook, Gmail, and a lot of other email products and services use SMTP. But to use blastula, you require access to send mail programmatically as a result of that server. Based on security options, you may not have that access — in particular at operate.
If you want to use a individual Gmail account, you have to set your account to let what Google considers “less secure” applications to access it. I never endorse accomplishing this for a most important Google account that has delicate facts.
For a secondary or otherwise unimportant account, go to Take care of your Google Account > Stability and scroll down to exactly where it claims “Less protected application access.” You are going to be warned not to do this. (And with very good rationale. I turned this setting back again off soon after writing this article.)
You can preserve your Gmail user name and server options with blastula’s generate_smtp_creds_essential()
operate. This will save your user name and company server options, and you’ll be requested to enter your password.
Here’s the structure for the smtp_send()
operate incuding individuals saved qualifications:
smtp_send(my_email_item,
from = "[email protected]",
to = "[email protected]",
subject = "Your email subject",
qualifications = creds_essential("gmail")
)
And there you have it — an straightforward way to share your R analysis with other folks. You can see it all in action in the movie embedded at the top of this web site.
For additional R ideas and tutorials, head to my Do Far more With R page.
Copyright © 2021 IDG Communications, Inc.