我正在尝试使用SendMailR发送数据帧。我可以将其作为附件发送,格式合理。但是我想在电子邮件正文中发送数据帧。我尝试了capture.output,print,sprintf,但什至无法关闭格式。
例如我尝试了以下语法
for (i in 1:nrow(df)){
MSG = c(MSG,rownames(df)[1],as.character(unlist(df[i,])),'\n')
}
MSG = sprintf('%-10s',MSG)
sendmail(from,to,subject,msg = list(MSG,attachment1,attachment2 ... ))
换句话说,我认为可能有必要将我的数据框转换为具有/ n和sprintf('s-10%')等格式,并将其存储在MSG中。有人可以指出我正确的方向吗?
尽管发送HTML邮件sendmailR
不是一件容易的事,但是基于与包作者去年的邮件讨论(可以再次感谢Olaf Mersmann的帮助),这是可能的-只需覆盖Content-Type
标头即可。例如:
msg <- mime_part('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>HTML demo</title>
<style type="text/css">
</style>
</head>
<body>
<h1>HTML demo</h1>
</body>
</html>')
## Override content type.
msg[["headers"]][["Content-Type"]] <- "text/html"
from <- '<[email protected]>'
to <- "<[email protected]>"
subject <- "HTML test"
body <- list(msg)
sendmail(from, to, subject, body, ...)
另一方面,HTML真正不需要以表格或data.frame
人类可读格式显示表格。例如,有可以将R对象变为markdown的ascii
包或我的pander
pkg。快速演示:
> library(pander)
> panderOptions('table.split.table', Inf)
> pander(head(iris, 3))
-------------------------------------------------------------------
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
-------------- ------------- -------------- ------------- ---------
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
-------------------------------------------------------------------
> pander(head(iris, 3), style = 'grid')
+----------------+---------------+----------------+---------------+-----------+
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
+================+===============+================+===============+===========+
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
+----------------+---------------+----------------+---------------+-----------+
| 4.9 | 3 | 1.4 | 0.2 | setosa |
+----------------+---------------+----------------+---------------+-----------+
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
+----------------+---------------+----------------+---------------+-----------+
如果要将其连接到电子邮件正文,请使用pander.return
代替它返回字符向量,而不是写到控制台。并且还有其他一些可用的表style
,也很有用,panderOptions
例如用于设置小数点,日期格式等:http : //rapporter.github.io/pander/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句