4

I'm hoping to generate an email once my markdown has been completed, with some data and a few ggplots embedded in the body of the email.

I've been able to get it to produce text I want using the code below, however I've been unable to get it to insert the 3 ggplots I need into the body as images (or in any form by that matter) underneath the text I've got.

subject_1 <- "Time Update"
subject_2 <- paste0('(', current_time_round,')')

email_body <- paste("<div style='font-family:calibri'><p>Hi</p>
<p>Please see below for the Time update. Data was calculated at", current_time_round,"</p>",
    "<li>", "Specific Vol:", paste0(specific_vol, "%</li>"), "</div>")

date <- as.Date(Sys.time())
email_subject <- paste(subject_1, date, subject_2)

OutApp <- COMCreate("Outlook.Application")
Email = OutApp$CreateItem(0)
Email[["to"]] = target
Email[["subject"]] = email_subject

Email[["htmlbody"]] = email_body
Email$Display()

The ggplots are stored as objects with the names, "rz", "rs", & "rp". I've tried a number of different methods, including this one: RDCOMClient (Outlook) - ggplot

However that just delivers an error on the images and seems to interfere with the data. Any advice on how I can get it working would be very appreciated.

Waldi
  • 39,242
  • 6
  • 30
  • 78
alec22
  • 735
  • 2
  • 12
  • I wasn't able to install / test `RDCOMClient` on R>=4.0, hence `blastula` solution, but this might be helpful : https://www.seancarney.ca/2020/10/10/advanced-email-in-r-embedding-images-and-markdown/ – Waldi Mar 18 '22 at 17:55

2 Answers2

4

The blastula package from RStudio allows to compose mails with text and plots.

You could create a custom email.Rmd file:

---
output: blastula::blastula_email 
--- 
Please see below for the time update. 
  
Data was calculated at `r calculationtime`

```{r echo=FALSE}
ggplot(mtcars)+geom_point(aes(x=mpg,y=hp))
```

Note that in comparison to standard RMarkdown, you can pass the libraries (here ggplot2) and the variables (here calculationtime) directly in the calling script, see below.

This worked on an Outlook server, after setting up the SMTP credentials :

library(blastula) 
library(keyring)
library(ggplot2)

# to be defined beforehand
create_smtp_creds_key(
  id = "test",
  host = "smtp.host.address",
  use_ssl = F,
  port=25,
  overwrite=T
)

# knit mail
calculationtime <- Sys.time()
email <- render_email(input = 'email.Rmd')
# email # uncomment to check body of email in Viewer pane

# send mail
email %>%
  smtp_send(
    from = "test@test.com",
    to = "otheruser@test.com",
    subject = "Time update",
    credentials = creds_key(id = "test")
  )

enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78
2

This should also be doable with the emayili package with similar syntax, e.g.

library(emayili)
calculationtime <- Sys.time()
email <- envelope() %>%
  subject(paste0("Time Update ", Sys.Date(), " ", format(Sys.time(), "%H:%M"))) %>%
  to(target) %>%
  render("email.Rmd")

https://datawookie.github.io/emayili/

Jon Spring
  • 55,165
  • 4
  • 35
  • 53