{emayili} Rendering R Markdown
Published: 2021-09-18 05:55:43

---
title: {emayili} Rendering R Markdown
published: true
date: 2021-09-17 00:00:00 UTC
tags: 
canonical_url: https://datawookie.dev/blog/2021/09/emayili-rendering-r-markdown/
---

![{emayili} Rendering Plain Markdown](https://datawookie.dev/blog/2021/09/emayili-rendering-r-markdown/featured.jpg)

In a [previous post](../emayili-rendering-plain-markdown/) 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](https://cran.r-project.org/package=mailmerge), 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.

![](https://datawookie.dev/blog/2021/09/emayili-rendering-r-markdown/gh-doc.png)

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](https://fathomdata.gitlab.io/library/presentation/20210916-birminghamr-meetup/20210916-birminghamr-emayili.pdf).

<iframe src="https://www.youtube.com/embed/9Qp1SPFuGfA" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="YouTube Video"></iframe>