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

时间:2014-05-05 14:42:23

标签: r pander sendmailr

我有这个数据框:

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")

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

我用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)

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

2 个答案:

答案 0 :(得分:4)

你是什么意思?

  

表格看起来很简单

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


HTML版的快速演示:

  1. 初始化所需的R包:

    library(sendmailR)
    library(xtable)
    
  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>'))
    
  3. 使用undocumented hack覆盖content-type

    msg[["headers"]][["Content-Type"]] <- "text/html"
    
  4. 使用给定主题将邮件发送给指定的收件人:

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

  5. 结合降价和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))
    

    这里的诀窍是使用内联CSS将font-family设置为monospace,同时用不间断的空格替换文档中的所有空格。另一个(更确切地说是elegnat)解决方法可能是在pre 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><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))
    

答案 1 :(得分:0)

如果dput是您的data.frame。

我用它。

Date=sys.Date()-1
date2 <- paste("My subject of mail", Date, sep = " - ")
setwd("/xyz")
newdir <- paste("output", Sys.time(), sep = "_")
dir.create(newdir)#, showWarnings = FALSE)
setwd(newdir)

######
mydoc = bsdoc( title = 'my document')
options( "ReporteRs-fontsize" = 8 )
mydoc = addParagraph(mydoc, value = "Hi All, \n\nPlease check attached summary.")
mydoc = addParagraph(mydoc, value = "Summary:")
MyFTable = FlexTable( data = dput, add.rownames = FALSE, header.cell.props = cellProperties( background.color = "#FAEBD7" )
                      , header.par.props = parProperties(text.align = "center" ))
MyFTable = setColumnsColors( MyFTable, j=1, colors = '#F0F8FF' )
MyFTable[ , ] = parProperties( text.align = 'center')
MyFTable = setColumnsColors( MyFTable, j=ncol(dput), colors = '#F0F8FF' )
mydoc = addFlexTable( mydoc, MyFTable )
writeDoc( mydoc, file = "op2.html" )

send.mail(from = "abc@xyz.com",
          to = c("abc@xyz.com"),
          subject = date2,
          body = "op2.html",
          html = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "abc@xyz.com", passwd = "xyz@123", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

但我正在寻找发送邮件的更好选择,例如邮件正文中的图像或格式良好/压缩的图像。