我开发了一份报告,该报告大量使用了 RMarkdown v2 中的功能,尤其是将 css 类和 id 添加到 html 文档以便更好地控制使用样式表的输出的功能。我希望在电子邮件正文中发送这些报告。我一直在尝试使用 send.mail (mailR) 来做到这一点。根据他们的 gitgub 自述文件 ( 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...
问题是 knit2html 似乎仍在使用 RMarkdown v1,它不支持将 css 类和 id 添加到文档的语法。是否有任何其他解决方法,例如使用 rmarkdown::render 并以某种方式传递 options 参数?或者 knitr 是否有使用 RMarkdown v2 的时间表?
这可以重现如下:
ExampleStyles.css
.GreenItalic {
font-style: italic;
color: green;
}
例子.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。
最佳答案
knitr::knit2html()
仅用于记录的 R Markdown v1。 knit2html()
的帮助页面中也记录了您应该使用 rmarkdown::render()
来呈现 R Markdown v2 文档。
要关闭 base64 编码,您可以在 YAML 元数据中使用选项 self_contained: no
,例如
---
output:
html_document:
self_contained: no
---
关于css - 在 R 中有什么方法可以将 RMarkdown v2 html 文件作为电子邮件的正文发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520928/