3

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

mailR 目前不支持解析使用数据 URI 方案编码的内联图像。请改用以下解决方法:

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

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

现在您可以通过 mailR 发送生成的 HTML 文件...

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

这可以复制如下:

ExampleStyles.css

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

Example.Rmd

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

# Heading { .GreenItalic }

使用 RStudio 编织(渲染)时,输出与预期一致。“标题”是斜体和绿色。

要通过电子邮件发送,可以使用以下代码:

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}”。据我所知,这是因为 knitr 使用的是 RMarkdown v1。

4

2 回答 2

4

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
---
于 2015-09-16T18:07:11.310 回答
0

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.

于 2018-08-06T11:03:44.720 回答