ShuN
ShuN

Reputation: 65

Is it possible to send visualizations through RDCOM?

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"]] = "[email protected]"
outMail[["subject"]] = "R Test"
outMail[["body"]] = "Hello"                   
outMail$Send()

Upvotes: 3

Views: 1425

Answers (2)

Emmanuel Hamel
Emmanuel Hamel

Reputation: 2233

The approach above is working. However, you have to allow the download of the image in the email. With the approach below, you do not have to allow the download of the image to see the image in the email's body :

library(RDCOMClient)
path_To_Image <- "D:/images.jpg"
Outlook <- RDCOMClient::COMCreate("Outlook.Application")
Email <- Outlook$CreateItem(0)
Email[["to"]] <- "[email protected]"
Email[["subject"]] <- "Test"
Email[["attachments"]]$Add(path_To_Image, 1, 0)
Email[["htmlbody"]] <- "<html><p>Test 1.</p> <img src=cid:images.jpg height=520 width=750>"
Email$Send()

Upvotes: 0

Ian Wesley
Ian Wesley

Reputation: 3624

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"]] = "[email protected]"
outMail[["subject"]] = "R Test"
outMail[["HTMLbody"]] =  MyHTML                  
outMail$Send()

Upvotes: 2

Related Questions