I have the following well formatted table created using rmarkdown and saved as table.rmd file.
library(RDCOMClient)
kable(mtcars[1:5, 1:6]) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = T,
position = "left",
font_size = 13,
fixed_thead = list(enabled = T, background = "#c5d9f1")) %>%
column_spec(1, bold = T, border_right = T) %>%
column_spec(2, width = "5cm", background = "yellow") %>%
row_spec(4:5, bold = T, color = "white", background = "grey")
Now, I want to use the following code to send this file/table over the outlook as an email body, while retaining the original formatting of the table.
rmarkdown::render("table.Rmd", "html_document")
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "email@abc.com"
outMail[["subject"]] = paste0("Report ", Sys.Date() - 1)
df_html <- read table.html as html so that the df_html gets correctly displayed as well formatted html table.
outMail[["HTMLBody"]] = df_html
outMail$Send()
How should I do that? My belief is if I can read the table.html as html itself in R, I can do this. So, if that's correct, how can I create the df_html that I can assign to outMail[["HTMLBody"]] to hopefully make it work?
I have been able to do it with the following code:
library(RDCOMClient)
library(kableExtra)
send_email <- function(vec_to = "",
vec_cc = "",
vec_bcc = "",
char_subject = "",
char_body = "",
char_htmlbody = "",
vec_attachments = "") {
Outlook <- RDCOMClient::COMCreate("Outlook.Application")
Email <- Outlook$CreateItem(0)
Email[["to"]] <- vec_to
Email[["cc"]] <- vec_cc
Email[["bcc"]] <- vec_bcc
Email[["subject"]] <- char_subject
if (char_body != "" && char_htmlbody != "") {
stop("Error")
}
if (char_htmlbody == "") {
Email[["body"]] <- char_body
} else {
Email[["htmlbody"]] <- char_htmlbody
}
if (vec_attachments[1] != "") {
for (i in seq_along(vec_attachments)) {
Email[["attachments"]]$Add(vec_attachments[i])
}
}
Email$Send()
}
html_Table <- (kable(mtcars[1 : 5, 1 : 6]) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = TRUE, position = "left", font_size = 13,
fixed_thead = list(enabled = TRUE, background = "#c5d9f1"))
%>% column_spec(1, bold = TRUE, border_right = TRUE)
%>% column_spec(2, width = "5cm", background = "yellow")
%>% row_spec(4 : 5, bold = TRUE, color = "white", background = "grey"))
html_Table <- html_Table[[1]]
send_email(vec_to = "emmanuel.hamel.1@ulaval.ca", vec_cc = "", vec_bcc = "",
char_subject = "", char_body = "", char_htmlbody = html_Table,
vec_attachments = "")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.