1

I am trying to send a mail via mailR and it is working fine. I have a Data Frame and I wanted to colour code specific cells. I used Kable() to format and I got the desired output and it is showing the way it should in Rstudio viewer. But while sending that HTML in the mail, the gridlines are not visible.

I've tried adding 'bordered' in kable_styling()

#color coding for a data frame
library(knitr)
library(kableExtra)

library(dplyr)

a<-mtcars[1:10, 1:2] %>%
        mutate(
                car = row.names(.),
                mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20,                     "red", "blue")),
                cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45, 
                                background = factor(cyl, c(4, 6, 8), 
                                                    c("#666666",     "#999999", "#BBBBBB")))
        ) %>%
        select(car, mpg, cyl) %>%
        kable(format = "html", escape = F) %>%
        kable_styling(c("striped","bordered"), full_width = F)


#=================Send Email
library(mailR)
body_B <- paste("<p>
                ",a,"
                <br> Note: report

                <p>",sep="")


Subject <- paste(Sys.Date(), 'xyz',sep= ":")

send.mail(from = "asdf@gmail.com", 
          to = c("asdf@gmail.com"),
          subject = Subject,        
          body = body_B,
          html = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 587,
                      user.name = "#####",
                      passwd = "#####", ssl = T), 
          authenticate = T,
          #attach.files = raw_data,
          send = TRUE)

1 Answers1

0

When you print a, you're using the print.kableExtra method, but body_B doesn't have the kableExtra class, so you'll just use the default methods producing the body for your message. If you read the source to kableExtra:::print.kableExtra, you'll see it really does quite a bit of manipulation before sending the object to the browser, so you'll need to duplicate that.

Here's an attempt at that. This might not be the simplest way to do it, but this produces a file that displays properly:

# Generate an HTML header
html_header <- htmltools::tags$head(rmarkdown::html_dependency_jquery(), 
                                    rmarkdown::html_dependency_bootstrap(theme = "simplex"), 
                                    html_dependency_kePrint())
# Declare body_B to be HTML
html_table <- htmltools::HTML(body_B)

# Glue the two parts together
html_result <- htmltools::tagList(html_header, html_table)

# Create a temp file and write the result there
html_file <- tempfile(fileext = ".html")
htmltools::save_html(html_result, file = html_file, background = "white")

# That will require several different files.  You probably want to 
# merge them all into one to display.  Pandoc can do that...
system(paste(rmarkdown::pandoc_exec(), "--self-contained --template", html_file, "-o", html_file, "</dev/null"))

# The result is now in the file named in html_file.  If you want it as
# a character variable, you can read it.
html_lines <- readLines(html_file)

I haven't tried putting this in an email, but I don't see why it wouldn't work.

user2554330
  • 37,248
  • 4
  • 43
  • 90