In R is there any way to send an RMarkdown v2 html file as the body of an email

自闭症网瘾萝莉.ら 提交于 2019-12-12 13:52:48

问题


I have developed a report that makes heavy use of the features in RMarkdown v2, especially the feature to add css classes and id's to html documents in order to have greater control over the output using style sheets. I wish to send these reports in the body of an email. I have been trying to do this using send.mail (mailR). According to their gitgub readme file (https://github.com/rpremraj/mailR/blob/master/README.md)

mailR does not currently support resolving inline images encoded using the data URI scheme. Use the workaround below instead:

First off, create the HTML file from the R terminal (the important thing here is that options does not include "base64_images" --- see ?markdown::markdownHTMLOptions):

library(knitr)
knit2html("my_report.Rmd", options = "")

Now you can send the resulting HTML file via mailR...

The problem is the knit2html seems to still use RMarkdown v1, which doesn't support the syntax for adding css classes and id's to documents. Is there any other workaround, for instance using rmarkdown::render and somehow passing through the options parameter? Or is there a timeline for knitr to use RMarkdown v2?

This can reproduced as follows:

ExampleStyles.css

.GreenItalic {
  font-style: italic;
  color: green;
}

Example.Rmd

---
output: html_document
css: ExampleStyles.css
---

# Heading { .GreenItalic }

When knitted (rendered) using RStudio, the output is as expected. "Heading" is in italics and green.

To send it by email the following code can be used:

library(mailR)
library(knitr)

ReportName <- "Example"
knit2html(paste0(ReportName, ".Rmd"), options = "", styles = "ExampleStyles.css")

send.mail(from = "RTestingTesting@gmail.com",
          to = "RTestingTesting@gmail.com",
          subject = "Subject",
          html = TRUE,
          inline = TRUE,
          body = paste0(ReportName, ".html"),
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "RTestingTesting", passwd = "Password", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

However in this case the output is "Heading { .GreenItalic}" in black and non-italic. As far as I'm aware this is because knitr is using RMarkdown v1.


回答1:


knitr::knit2html() is for R Markdown v1 only as documented. It is also documented in the help page of knit2html() that you should use rmarkdown::render() to render R Markdown v2 documents.

To turn off base64 encoding, you may use the option self_contained: no in the YAML metadata, e.g.

---
output: 
  html_document: 
    self_contained: no
---



回答2:


A workaround/solution I did was to set the param:

#------------------
markdownToHTML("MyReport.Rmd", output="MyReport.html", options=c("toc", "use_xhtml", "smartypants",  "mathjax", "highlight_code"))

send.mail(from = "myemail@example.com",
            to = c("myemail@example.com", 
                   "myotheremail@example.com"),
            subject = "Email with a Markdown document in HTML at the message body",
            body = "MyReport.html",
            html = TRUE,
            inline = TRUE,
            smtp = list(host.name = "localhost"),
            send = TRUE)
#------------------

(or choose your own param set for the options of markdownToHTML, while ensuring that you avoid adding the "base64_images")

This way, I managed to send the html and get the report to show in the body of the email the images included in your report. The images were places in the same folder where the html was generated.

I hope this helps.



来源:https://stackoverflow.com/questions/32520928/in-r-is-there-any-way-to-send-an-rmarkdown-v2-html-file-as-the-body-of-an-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!