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.
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 = "[email protected]",
to = "[email protected]",
subject = "Time update",
credentials = creds_key(id = "test")
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With