如何将数据框转换为表格以作为电子邮件正文发送

use*_*980 1 r sendmailr pander

我有这个数据框:

library(sendmailR)
library(pander)

dput(s)
structure(list(Description = c("ServerA", "ServerB", "ServerC", 
"ServerD", "ServerE", "ServerF"), Value = c("2", "2", "100", 
"100", "80", "20")), .Names = c("Description", "Value"), row.names = c(NA, 
6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

我想把这个数据框放在一个漂亮的表格中,然后通过电子邮件发送给一些人。

我用 pandoc 尝试过,但表格看起来很简单:

 t<-pandoc.table.return(s, caption="Server CPU Utilization")

    from <- "user@example.com"
    to <- c("end_users@example.com")
    subject <- paste(Sys.time()," Servers CPU utilization")
    body <- t                
    mailControl=list(smtpServer="mailhost.example.net")

    sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以将数据框格式化为漂亮的表格以作为电子邮件发送?表格必须在电子邮件正文中,而不是作为附件。

dar*_*zig 5

在下是什么意思?

桌子看起来很普通

如果您不喜欢默认的多行格式,您还可以为表格选择其他一些 Markdown 格式,例如传递style = 'grid'给。或者你的意思是桌子会用非等宽字体分崩离析/看起来很难看?结果将取决于电子邮件客户端,因此我宁愿选择发送 HTML 邮件并指定等宽字体系列,或者以 HTML 呈现表格。pandoc.table.return


HTML 版本的快速演示:

  1. 初始化所需的 R 包:

    library(sendmailR)
    library(xtable)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将静态部分与动态创建的 HTML 表连接起来,构建一个 HTML 正文:

    msg <- mime_part(paste('<!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"/>
    </head>
    <body>', print(xtable(s), type = 'html'), ',</body>
    </html>'))
    
    Run Code Online (Sandbox Code Playgroud)
  3. content-type使用未记录的 hack覆盖:

    msg[["headers"]][["Content-Type"]] <- "text/html"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用给定主题将邮件发送给您指定的收件人:

    from    <- '<foo@example.com>'
    to      <- '<bar@example.com>'
    subject <- 'HTML table in the body'
    body    <- list(msg)
    sendmail(from, to, subject, body)
    
    Run Code Online (Sandbox Code Playgroud)

结合 markdown 和 HTML 版本:

msg <- mime_part(paste('<!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"/>
</head>
<body><div style="font-family: monospace;">', gsub(' ', '&nbsp;', paste(pander.return(s, caption = "Server CPU Utilization", style = 'grid'), collapse = '<br>')), '</div></body>
</html>'))
msg[["headers"]][["Content-Type"]] <- "text/html"
sendmail(from, to, subject, list(msg))
Run Code Online (Sandbox Code Playgroud)

这里的技巧是使用内联 CSS设置font-familyto monospace,同时用不间断空格替换文档中的所有空格。另一个(而且更优雅)的解决方法可能是在preHTML 标签之间放置降价:

msg <- mime_part(paste('<!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"/>
</head>
<body><pre>', paste(pander.return(s, caption = "Server CPU Utilization", style = 'grid'), collapse = '\n'), '</pre></body>
</html>'))
msg[["headers"]][["Content-Type"]] <- "text/html"
sendmail(from, to, subject, list(msg))
Run Code Online (Sandbox Code Playgroud)