简体   繁体   English

R 使用 formattable 更改数据框 / HTML 表中文本的颜色

[英]R change colours of text in data frame / HTML table using formattable

I would like to add different colour text to a date frame or HTML table based on the following rules (which able to time to crack variable)我想根据以下规则将不同颜色的文本添加到日期框架或 HTML 表中(能够定时破解变量)

instant = red text, seconds = orange text, minutes - yellow text, years = green text.即时 = 红色文本,秒 = 橙色文本,分钟 - 黄色文本,年 = 绿色文本。

My data frame is我的数据框是

> dput(Final_DF)
structure(list(gender = c("female", "male", "male", "female", 
"female", "male", "male", "male", "male", "female", "male", "male", 
"female", "female", "female", "female", "male", "female", "male", 
"male", "female", "female", "female", "female", "female", "female", 
"male", "female", "female", "female", "female", "female", "female", 
"female", "male", "male", "female", "female", "male", "female", 
"female", "male", "female", "female", "male", "male", "male", 
"male", "male", "male"), age = structure(c(47L, 43L, 65L, 24L, 
44L, 60L, 26L, 25L, 62L, 23L, 44L, 61L, 27L, 47L, 18L, 23L, 34L, 
77L, 71L, 19L, 64L, 61L, 22L, 55L, 45L, 29L, 21L, 64L, 43L, 20L, 
32L, 55L, 68L, 21L, 81L, 43L, 63L, 72L, 38L, 20L, 66L, 39L, 64L, 
20L, 73L, 21L, 53L, 75L, 69L, 82L), class = c("variable", "integer"
), varname = "Age"), web_browser = structure(c(1L, 1L, 4L, 1L, 
3L, 3L, 2L, 1L, 4L, 1L, 1L, 1L, 3L, 4L, 1L, 2L, 1L, 3L, 3L, 2L, 
1L, 1L, 1L, 3L, 4L, 3L, 4L, 4L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 
1L, 2L, 3L, 4L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 4L, 1L), .Label = c("Chrome", 
"Internet Explorer", "Firefox", "Netscape"), class = c("variable", 
"factor"), varname = "Browser"), Pswd = c("Password", "abc1", 
"football", "Pr?hStMz3xuJ", "Strawberries", "JTA9SsTLjU", "Jazzed!", 
"D!gU4", "12345!", "mQf0J?2", "password1", "CATS?", "F!!FtBm", 
"!!!!", "HBpqA?xya9SIi", "Os4Ft%D", "fryj0t9KS", "monkey", "shadow", 
"gpbvYZxYLBq7P", "blackberries", "Abc1!", "p0$YpH4", "SLOTH", 
"Qwertyuiop", "SqbmZZ!abHj", "?Lnp6X6TNz", "boatstatecat", "shearer9", 
"1B!aKnQm", "JTA9SsTLjU", "DOGS", "sunshine", "pgJz8!Hdde", "qwerty", 
"1q2w3e4r5t", "flightrockcups", "ashley", "Htkv5TDS51", "C8cFMWH?a$S", 
"cheesesteak", "password!", "aircraft", "Se3PKKeg?dU", "iloveyou", 
"Bug!5$r", "123", "dragon", "superman", "password"), pswd_length = c(8L, 
4L, 8L, 12L, 12L, 10L, 7L, 5L, 6L, 7L, 9L, 5L, 7L, 4L, 13L, 7L, 
9L, 6L, 6L, 13L, 12L, 5L, 7L, 5L, 10L, 11L, 10L, 12L, 8L, 8L, 
10L, 4L, 8L, 10L, 6L, 10L, 14L, 6L, 10L, 11L, 11L, 9L, 8L, 11L, 
8L, 7L, 3L, 6L, 8L, 8L), Time_to_crack = c("instant", "instant", 
"instant", "857 billion years", "0.84 seconds", "0.02 seconds", 
"4.71 seconds", "5.46 minutes", "instant", "111 years", "instant", 
"6.11 seconds", "23 days", "0.1 seconds", "393 million years", 
"3 months", "49 years", "instant", "instant", "81 trillion years", 
"43.7 seconds", "19.62 seconds", "12 years", "1.58 seconds", 
"0.15 seconds", "23 thousand years", "2 million years", "11 hours", 
"5.81 seconds", "1 year", "4 thousand years", "0.09 seconds", 
"instant", "4 million years", "instant", "0.34 seconds", "23 hours", 
"instant", "41 centuries", "14 thousand years", "11.26 seconds", 
"instant", "0.3 seconds", "8 thousand years", "instant", "4 hours", 
"instant", "instant", "instant", "instant")), row.names = c(NA, 
-50L), class = "data.frame")

这是一个取自excel的例子,展示了我所追求的

我一直在看 formattable 并用它来创建看起来更用户友好的 HTML 表

I have found this formatable data frame walkthrough, but not sure how to apply it to my scenario我找到了这个可格式化的数据框演练,但不知道如何将它应用到我的场景中

This could be achieved like so:这可以像这样实现:

  1. Following the vignette define a formatter function using ifelse or using eg dplyr::case_when which defines the conditional styles to apply.在小插图之后,使用ifelse或使用例如dplyr::case_when定义格式化程序 function ,它定义了要应用的条件 styles。
  2. In formattable apply this formatter to your column Time_to_crackformattable中将此格式化程序应用于您的列Time_to_crack

    library(formattable)
    library(dplyr)
    
    time_formatter <- formatter("span", 
                                style = x ~ style(color = dplyr::case_when(
                                  grepl("(instant|seconds)", x) ~ "red",
                                  grepl("(months|years)", x) ~ "green", 
                                  grepl("hours", x) ~ "yellow", 
                                  grepl("minutes", x) ~ "orange",
                                  TRUE ~ "black")))
    
    formattable(Final_DF, list(Time_to_crack = time_formatter))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM