I'm attempting to generate an HTML document using R Markdown that includes charts and email it to a defined distribution group. I've had success doing this with just a knitr::kable, but as soon as I attempt to include an embedded plot, the email process fails.
I'm open to any suggestions on alternative ways to embed images in emails and have found a few threads on how to approach this issue here, but thus far I haven't been able to run a successful end to end test. For this use case, I'm using rmarkdown::render and the mailR package (I have to send to and from an Office 365 Outlook host).
here is my example.Rmd script
---
title: "Unemployment Report"
output:
html_document:
self_contained: no
theme: null
highlight: null
mathjax: null
css: ink.css
---
Annual Unemployment Summary
```{r, echo=FALSE}
library(tidyverse)
library(plotly)
library(lubridate)
library(knitr)
economics %>%
mutate(year = year(date)) %>%
group_by(year) %>%
summarise_if(is.numeric, funs(mean(.))) %>%
kable()
Unemployment Charts
charts <- c("pce", "pop", "psavert", "uempmed", "unemploy") %>%
map( ~{
economics %>%
plot_ly(type = "scatter", mode = "lines") %>%
add_trace(x = ~date, y = ~get(.x)) %>%
layout(
annotations = list(text = .x, xref = "paper", yref = "paper", yanchor = "bottom", xanchor = "center", align = "center", x = 0.5, y = 1, showarrow = FALSE),
showlegend = FALSE)
})
subplot(charts, nrows = length(charts), shareX = TRUE)
And here's a sample email script:
# compile using rmarkdown
library(rmarkdown)
rmarkdown::render("example.Rmd")
library(mailR)
send.mail(from = "me@company.com",
to = "me@company.com",
subject = "R Markdown Report - rmarkdown",
html = T,
inline = T,
body = "example.html",
smtp = list(host.name = "outlook.office365.com", port = 587, user.name = "me@company.com", passwd = "password", tls = T),
authenticate = T,
send = T)
Here's the error I'm getting:
org.apache.commons.mail.EmailException: Building the MimeMessage failed
at org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:112)
at org.apache.commons.mail.Email.send(Email.java:1436)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at RJavaTools.invokeMethod(RJavaTools.java:386)
Caused by: java.io.IOException: Cant resolve the following file resource :example_files\htmlwidgets-1.0\htmlwidgets.js
at org.apache.commons.mail.resolver.DataSourceFileResolver.resolve(DataSourceFileResolver.java:105)
at org.apache.commons.mail.resolver.DataSourceFileResolver.resolve(DataSourceFileResolver.java:79)
at org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:151)
at org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:106)