Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In a previous post I documented a new feature in {emayili}
, the ability to render Plain Markdown directly into the body of an email message.
Today I’m announcing the release of a new minor version, 0.5.0, in which {emayili}
is now able to render R Markdown into an email. This is a major leap forward for the package.
I should mention that this capability (rendering R Markdown into an email) is already available in the {mailmerge}
package, but that only works with Gmail.
Install & Load
You can install this version directly from GitHub as follows.
remotes::install_github("datawookie/emayili", ref = "v0.5.0")
This version was also published on CRAN on 17 September 2021.
Now load the package.
library(emayili) # A couple of options to display message body. # options( envelope_details = TRUE, envelope_invisible = FALSE )
Check on the installed version.
packageVersion("emayili") [1] '0.5.0'
A Simple R Markdown File
For the purposes of illustration let’s use a super simple R Markdown file, pi.Rmd
.
--- title: "Approximating Pi" output: html_document --- ```{r} 22 / 7 ```
Create an empty message object.
msg <- envelope()
And then render pi.Rmd
into the body of the message. In this case I’m going to set the include_css
option to FALSE
so that the copious volume of CSS is suppressed in the resulting HTML.
msg %>% render("pi.Rmd", include_css = FALSE) Date: Fri, 17 Sep 2021 10:16:09 GMT X-Mailer: {emayili}-0.5.0 MIME-Version: 1.0 Content-Type: multipart/related; boundary="b9173ea5" --b9173ea5 Content-Type: text/html; charset=utf-8 Content-Disposition: inline <html><body> <div class="container-fluid main-container"> <div id="header"> <h1 class="title toc-ignore">Approximating Pi</h1> </div> <pre class="r"><code>22 / 7</code></pre> <pre><code>[1] 3.142857</code></pre> </div> </body></html> --b9173ea5--
The document is inserted as a separate MIME chunk with type text/html
.
Let’s move onto a more interesting example.
R Markdown File from Template
Create an R Markdown file using the "github_document"
template.
rmd <- "gh-doc.Rmd" rmarkdown::draft( rmd, template = "github_document", package = "rmarkdown", edit = FALSE ) --- title: "Untitled" output: github_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## GitHub Documents This is an R Markdown format used for publishing markdown documents to GitHub. When you click the **Knit** button all R code chunks are run and a markdown file (.md) suitable for publishing to GitHub is generated. ## Including Code You can include R code in the document as follows: ```{r cars} summary(cars) ``` ## Including Plots You can also embed plots, for example: ```{r pressure, echo=FALSE} plot(pressure) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Now we’ll add some addresses to the message object and render the R Markdown.
msg %>% to("bob@google.com") %>% from("alice@google.com") %>% subject("Rendering an R Markdown Document") %>% render(rmd)
Dispatching the email results in the message below.
The message doesn’t need to look this generic. You can make your R Markdown documents as fancy as you want and {emayili}
will be happy to send them.
Conclusion
If you haven’t tried {emayili}
before, now is a great time to give it a shot. And if you use R for automated reporting, then being able to render R Markdown directly into an email should simplify your workflow.
I gave a talk about these new features (and most of the old features!) at the Birmingham R User Group. The slides are available here.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.