[英]R change colours of text in data frame / HTML table using formattable
我想根據以下規則將不同顏色的文本添加到日期框架或 HTML 表中(能夠定時破解變量)
即時 = 紅色文本,秒 = 橙色文本,分鍾 - 黃色文本,年 = 綠色文本。
我的數據框是
> 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")
這可以像這樣實現:
ifelse
或使用例如dplyr::case_when
定義格式化程序 function ,它定義了要應用的條件 styles。formattable
中將此格式化程序應用於您的列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.