使用SendMailR将数据框作为电子邮件正文中的表格发送

hjw*_*hjw 8 r sendmailr

我正在尝试使用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 ... ))
Run Code Online (Sandbox Code Playgroud)

换句话说,我认为可能需要将我的数据帧转换为带有/ n和sprintf('s-10%')等的格式并将其存储在MSG中.有人能指出我正确的方向吗?

dar*_*zig 17

虽然发送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    <- '<foo@example.com>'
to      <- "<bar@example.com>"
subject <- "HTML test"
body    <- list(msg)
sendmail(from, to, subject, body, ...)
Run Code Online (Sandbox Code Playgroud)

另一方面,实际上不需要HTML来呈现表格或data.frame以人类可读的格式.例如ascii包或我的panderpkg可以将R对象转换为markdown.快速演示:

> 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   |
+----------------+---------------+----------------+---------------+-----------+
Run Code Online (Sandbox Code Playgroud)

如果要将其连接到电子邮件正文,请使用pander.return返回字符向量而不是写入控制台.还有一些其他可用的表style,也有一些有用的panderOptions例如设置小数点,日期格式等:http://rapporter.github.io/pander/


Ami*_*han 6

库"xtable"将有助于将数据框作为表附加到电子邮件中.试试这个

   library(xtable)

   body=print(xtable(dataframe,caption = "Heading for the table"), type="html", caption.placement = "top")
Run Code Online (Sandbox Code Playgroud)

  • 我正在努力解决同样的问题.认为我的病情会得到一些帮助. (4认同)