3

I am relatively new to R programming and have undertaken a little side project to introduce myself to the world of R. What I would like to do is help one of my colleagues automate a manual email process that he does each week.

The email consists of a chart created in excel, DOW index prices, our company's stock prices, and some commentary that he manually updates each week.

I have figured out how to use the RDCOMClient package to send emails but what I would like to do is integrate into the body of the email (in HTML format if possible) the charts and stock prices that he also pulls. I am hoping to automate all of this so all he has to do is update commentary and run the script.

The key limiting factor here is the target audience, this will be going out to executives who really do not like having to open email attachments. They want to open an email on their phone, get the relevant information, and move on.

This is what my program looks like so far:

library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "R Test"
outMail[["body"]] = "Hello"                   
outMail$Send()
ShuN
  • 65
  • 6

1 Answers1

2

Sure, first you save your image. Then use HTMLbody to insert the image using HTML code as follows:

library(htmlTable)

png("pictest.png")
plot(iris$Sepal.Length)
dev.off()

StockPrice <- "25.25"

MyHTML <- paste0("<html><p>This is a picture.</p> 
<img src='C:/Users/iwes/Desktop/RWorkingFolder/pictest.png' >
<p> Our StockPrices is: $", StockPrice,
"<p>here is a table:</p>",
htmlTable(head(iris,5)))

library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "R Test"
outMail[["HTMLbody"]] =  MyHTML                  
outMail$Send()
Ian Wesley
  • 3,565
  • 15
  • 34
  • Would it be possible to insert something other than an image file? If I wrote a program to chart the stock price over the past week could I call on that visual in the body of the email? – ShuN May 24 '17 at 15:40
  • You could put any valid HTML into the email (although Outlook/email security would be an issue for some things. For visualizations the first step would be to save the visualization as a standard image format. Other content can be put into the HTML code as well for example variables, there are several packages that make nice look html tables, or other thing. I will add a bit to this example. – Ian Wesley May 24 '17 at 15:51
  • Let me know if you have other questions. – Ian Wesley May 24 '17 at 15:58
  • Thanks Ian, that is exactly what I was looking for! – ShuN May 24 '17 at 20:14