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

喜夏-厌秋 提交于 2019-12-07 04:37:40

问题


I'm working with the 'pander' and 'sendmailr' packages to send a small data frame in the body of an email, rather than as an attachment. I'd like to send it from and to a gmail account.

I'm close, but the column headers won't align with the columns themselves in the email body the way they do in Rstudio for example- basically the column headers are too wide to line up with the data columns below them.

It seems the problem is the way the dashes and whitespaces are compressed in various email clients (I tried this in gmail, yahoo and hotmail through the web and through the email client that ships with OS X Mavericks). I was able to remedy the problem in my OS X email client by going to 'preferences' and checking the box labeled 'use fixed-width font for plain-text messages' but I'd like it to work on multiple devices, with multiple clients, etc for many of my coworkers so I'm wondering if there's a way that doesn't involve global email settings.

Here is the code to reproduce the problem:

library(sendmailR) # for emails from R
library(pander) # for table-formatting that does not require HTML

results <- head(iris)
pander(results) # widths look great so far...
a = pandoc.table.return(results)
strsplit(a, "\n") # widths still look great...

panderOptions('table.split.table', Inf) # show all columns on same line

msg_content <- mime_part(
  pandoc.table.return(results, style = "multiline")
)

# I'm using my own gmail address for email_from and email_to
sendmail(from = email_from,
         to = email_to,
         subject = "test",
         msg = msg_content
)

… and the email received has the problem described above.

Next you can see an image which illustrates the problem:


回答1:


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.



来源:https://stackoverflow.com/questions/27698252/column-widths-not-aligned-with-table-data-in-pander-tables-sent-from-r-with-send

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