DEV Community

Andrew B. Collier
Andrew B. Collier

Posted on • Originally published at datawookie.dev on

{emayili} R Markdown Parameters

{emayili} R Markdown Parameters

I don’t frequently use parameters in R Markdown documents. So the initial implementation of render() in {emayili} did not cater for them. A small tweak makes it possible though.

You can install the update from GitHub as follows.

remotes::install_github("datawookie/emayili", ref = "v0.5.2")
Enter fullscreen mode Exit fullscreen mode

Then load the package.

library(emayili)

# A couple of options to display message body.
#
options(
  envelope.details = TRUE,
  envelope.invisible = FALSE
)
Enter fullscreen mode Exit fullscreen mode

Create an empty message object.

msg <- envelope()
Enter fullscreen mode Exit fullscreen mode

Suppose you have the following simple R Markdown document, params.Rmd, which you want to render into the body of an email.

--------
title: "Message with Parameter"
output: html_document
params:
  value: 13
--------

The value is `r params$value`.
Enter fullscreen mode Exit fullscreen mode

We’ll start by rendering it using the default parameter value.

msg %>%
  subject("Default parameter value") %>%
  render("params.Rmd", include_css = FALSE)

Date: Mon, 20 Sep 2021 08:20:59 GMT
Subject: Default parameter value
X-Mailer: {emayili}-0.5.2
MIME-Version: 1.0
Content-Type: multipart/related; boundary="9554b959"

--9554b959
Content-Type: text/html; charset=utf-8
Content-Disposition: inline

<html>
<head></head>
<body>
<div class="container-fluid main-container">
<div id="header">
<h1 class="title toc-ignore">Message with Parameter</h1>
</div>
<p>The value is 13.</p>
</div>
</body>
</html>

--9554b959--
Enter fullscreen mode Exit fullscreen mode

As expected, the default value for the parameter comes through.

Now let’s test a different value.

msg %>%
  subject("Better parameter value") %>%
  render("params.Rmd", params = list(value = 42), include_css = FALSE)

Date: Mon, 20 Sep 2021 08:20:59 GMT
Subject: Better parameter value
X-Mailer: {emayili}-0.5.2
MIME-Version: 1.0
Content-Type: multipart/related; boundary="a099637d"

--a099637d
Content-Type: text/html; charset=utf-8
Content-Disposition: inline

<html>
<head></head>
<body>
<div class="container-fluid main-container">
<div id="header">
<h1 class="title toc-ignore">Message with Parameter</h1>
</div>
<p>The value is 42.</p>
</div>
</body>
</html>

--a099637d--
Enter fullscreen mode Exit fullscreen mode

🚀 Success: the specified parameter value now appears. If you use parameters in your R Makrkdown documents, then you can be confident that these will now work in {emayili} too.

Discussion (0)