Column widths do not match tabular data in pander tables sent from R using sendmailr

I work with the "pander" and "sendmailr" packages to send a small data frame in the body of the message, and not as an attachment. I would like to send it to my gmail account as well.

I’m close, but the column headers will not align with the columns themselves in the body of the message in the same way as in Rstudio, for example, basically the column headers are too wide to align the data columns below them.

The problem seems to be that dashes and spaces are compressed in different mail clients (I tried this in gmail, yahoo and hotmail over the Internet and through the mail client that comes with OS X Mavericks). I was able to fix this problem in my OS X mail client by going to "settings" and checking the "Use fixed-width font for text messages" checkbox, but I would like it to work on multiple devices, multiple clients, etc. for many of my colleagues, so I wonder if there is a way that is not related to 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 received message has the problem described above.

Next, you can see an image that illustrates the problem:

Described problem

+5
source share
1 answer

The problem with regular text messages and using markup tables is that the email client usually displays text in a fixed font, and you need to use your own settings throughout your email client to override this (for example, with your OS X email client). On the other hand, why are HTML trend letters :)

So, let's create HTML mail and include the label table in the 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 an error in sendmailR we must override the Content-type in HTML:

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

And now it is ready to post through the comment that you used in your example, resulting in:

Pander markdown table in HTML mail

The table should look just as good in any other HTML mail client. Please note that in this way you can also use HTML tables instead of markdowns if this is better suited to your needs.

+6
source

Source: https://habr.com/ru/post/1210079/


All Articles