Search code examples
rformattable

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)

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")

This is an example taken from excel, showing what I am after

I have been looking at formattable and have used this to create HTML table which looks more user friendly

I have found this formatable data frame walkthrough, but not sure how to apply it to my scenario


Solution

  • This could be achieved like so:

    1. Following the vignette define a formatter function using ifelse or using e.g. dplyr::case_when which defines the conditional styles to apply.
    2. In formattable apply this formatter to your column 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))
    
    

    enter image description here