I am trying to send a dataframe with SendMailR. I can send it as an attachment with reasonably good formatting. However I would like to send the dataframe in the body of the email. I tried capture.output, print, sprintf but am not even able to get the format close.
e.g. I tried the following syntax
for (i in 1:nrow(df)){
MSG = c(MSG,rownames(df)[1],as.character(unlist(df[i,])),'\n')
}
MSG = sprintf('%-10s',MSG)
sendmail(from,to,subject,msg = list(MSG,attachment1,attachment2 ... ))
In other words, I am thinking that it might be necessary to convert my dataframe into a format with /n and sprintf('s-10%') etc and store it in MSG. Can someone point me in the right direction?
library "xtable" will help to attach a data frame as table in email. Try this
Although sending HTML mails with
sendmailR
is not straighforward, but possible based on a mail discussion with the package author last year (thanks again to Olaf Mersmann for his kind help) - with simply overriding theContent-Type
header. E.g.:On the other hand, there is no real need for HTML to present tables or a
data.frame
in a human-readable format. There is e.g. theascii
package or mypander
pkg that can turn R objects to markdown. Quick demo:If you want to concatenate this to the e-mail body, use
pander.return
instead that returns character vector instead of writing to the console. And there are some other available tablestyle
s, also some usefulpanderOptions
e.g. to set decimal mark, date format etc: http://rapporter.github.io/pander/