Column widths not aligned with table data in pander tables sent from R with sendmailr

孤街浪徒 提交于 2019-12-05 11:39:30
daroczig

The problem with plain text e-mails and using markdown tables is that the e-mail client usually displays the text with a non-fixed font, and you have to use custom settings in all your e-mail client to override that (like you did with your OS X e-mail client). On the other hand, that's why HTML mails are trending :)

So let's create a HTML mail and include the markdown table in a pre block:

msg_content <- 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(results, style = "multiline"), collapse = '\n'), '</pre></body>
</html>'))

Due to a bug in sendmailR, we have to override the Content-type to HTML:

msg_content[["headers"]][["Content-Type"]] <- "text/html"

And now it's ready to be sent via the comment you used in your example, resulting in:

The table should look similarly fine in any other HTML-capable e-mail client. Please note that this way you could also use HTML tables instead of markdown if that would fit your needs better.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!