R语言 如何将表格中包含110的单元格的颜色更改为红色,将3更改为蓝色?

az31mfrm  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(100)
library(mailR) 
library(htmlTable)

x <- head(mtcars) y <- htmlTable(x, rnames = FALSE)

html_body <- paste0("<html><head>
               <style>
               body{font-family:Calibri, sans-serif;}
               table{border-left:1px solid #000000;border-top:1px solid #000000;}
               table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:13px; font-weight:bold; margin: 0px; padding-left: 5px; padding-right: 5px; margin: 0px;}
               table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:13px; font-weight:normal; margin: 0px; padding-left: 5px; padding-right: 5px; margin: 0px;}
               </style>
               </head><body><p> This is a test email. Ignore it.</p>",
               y, 
               "</body></html>")

sender <- "[email protected]" recipients <- c("[email protected]")

send.mail(from = sender,
          to = recipients,
          subject = "Test Email",
          body = html_body,
          smtp = list(host.name = "smtp.gmail.com",
                      port = 465, 
                      user.name = "[email protected]",            
                      passwd = "PASSWORD",
                      ssl = TRUE),
          authenticate = TRUE,
          html = TRUE,
          send = TRUE)

请在这方面帮助我。我想用绿色突出显示值110,用红色突出显示3,用黄色突出显示所有其他值。

njthzxwz

njthzxwz1#

您可以在'htmlTable'中使用CSS:

red <- function(x) { 
 sprintf("<span style='color: red;'> %s </span>", x)
}
green <- function(x) { 
  sprintf("<span style='color: green;'> %s </span>", x)
}
yellow <- function(x) { 
  sprintf("<span style='color: yellow;'> %s </span>", x)
}

x <- head(mtcars)

x <- apply(x, 1:2, function(x) {
  if(x == 110){
    x <- red(x)
  } else if (x == 3) {
    x <- green(x)
  } else {
    x <- yellow(x)
  }
})

library(htmlTable)
htmlTable(x, escape.html = FALSE)

相关问题