{emayili} Rendering R Markdown

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. Also, this post shows how something similar can be done with the {blastula} package, but IMHO the implementation in {emayili} is a lot simpler.

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:                         Sat, 12 Feb 2022 07:56:36 GMT
X-Mailer:                     {emayili}-0.7.5
MIME-Version:                 1.0
Content-Type:                 text/html; 
                              charset=utf-8
Content-Disposition:          inline

<html>
<head>
<title>Approximating Pi</title>
</head>
<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>

The document is inserted with MIME 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
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
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```r
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.

An email message with contents rendered from R Markdown (viewed in Gmail web client).

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.