Email dataframe as table in email body with SendMailR

自古美人都是妖i 提交于 2019-11-28 06:37:30

Although sending HTML mails with sendmailR is not straighforward, but possible based on a mail discussion with the package author last year (thanks again to Olaf Mersmann for his kind help) - with simply overriding the Content-Type header. E.g.:

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, ...)

On the other hand, there is no real need for HTML to present tables or a data.frame in a human-readable format. There is e.g. the ascii package or my pander pkg that can turn R objects to markdown. Quick demo:

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

If you want to concatenate this to the e-mail body, use pander.return instead that returns character vector instead of writing to the console. And there are some other available table styles, also some useful panderOptions e.g. to set decimal mark, date format etc: http://rapporter.github.io/pander/

library "xtable" will help to attach a data frame as table in email. Try this

   library(xtable)

   body=print(xtable(dataframe,caption = "Heading for the table"), type="html", caption.placement = "top")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!