在R有任何方式发送RMarkdown v2 html文件作为电子邮件的正文 [英] In R is there any way to send an RMarkdown v2 html file as the body of an email

查看:1789
本文介绍了在R有任何方式发送RMarkdown v2 html文件作为电子邮件的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个报告,大量使用RMarkdown v2中的功能,特别是添加CSS类和id到html文档的功能,以便更好地控制输出使用样式表。我希望在电子邮件正文中发送这些报告。我一直在试图这样做使用send.mail(mailR)。根据他们的gitgub自述文件( https://github.com/rpremraj/mailR/ blob / master / README.md

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目前不支持解析使用数据URI方案编码的内联图像。请使用下面的解决方法:

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

首先,从R终端创建HTML文件(重要的是这里的选项不包括base64_images---见?markdown :: markdownHTMLOptions):

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 = "")




生成的HTML文件通过mailR ...

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

问题是knit2html似乎仍然使用RMarkdown v1,它不支持用于将css类和id添加到文档的语法。是否有任何其他解决方法,例如使用rmarkdown :: render和某种方式通过选项参数?或者有knitr使用RMarkdown v2的时间线?

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?

这可以复制如下:

ExampleStyles。 css

ExampleStyles.css

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

Example.Rmd

Example.Rmd

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

# Heading { .GreenItalic }

使用RStudio编织(渲染)时,输出与预期的一样。 标题以斜体和绿色显示。

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)

但是在这种情况下,输出是Heading {.GreenItalic} -italic。据我所知,这是因为knitr正在使用RMarkdown v1。

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.

推荐答案

knitr :: knit2html()是R Markdown v1的文档。它还在 knit2html()的帮助页面中记录,您应该使用 rmarkdown :: render() R Markdown v2文档。

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.

要关闭base64编码,您可以在YAML中使用选项 self_contained:no 元数据,例如

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
---

这篇关于在R有任何方式发送RMarkdown v2 html文件作为电子邮件的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆