Formattable absolute values (ignoring sign)

123 Views Asked by At

I'm trying to use the color_tile on column 'a' and I'm trying to find out how to either ignore the sign, or use the style from column 'c' (the absolute values of a, hiding c after with c=FALSE).

I've seen examples where it uses column 'c' but it uses the actual values inside if statements rather than the style, or the numbers with no manual input. So what I want is for -5 to have the darkest green, and 1 to be without color.

What I have is

formattable(table, list(a=color_tile("transparent", "green"), 
b=color_tile("transparent","green"),
c=color_tile("transparent","green")))

Can anyone help?

what I currently have

Edit data:

table <- data.frame("a"=c(1,-5,3,-2), "b"=c(4,-2,1,5), "c"=c(1,5,3,2))
1

There are 1 best solutions below

0
On BEST ANSWER

Formatter based own column value

formatter_tile = formatter("span",
                 style = x ~ style(display = "block", 
                                padding = "0 4px", 
                                `border-radius` = "4px", 
                                `background-color` = ifelse(x == 1, 
                                                            "transparent",
                                                            csscolor(gradient(as.numeric(abs(x)), "lightgreen", "darkgreen")))))

Formatter based on another column value (b based on c)

formatter_tile_from_c = formatter("span",
                                   style = x ~ style(display = "block", 
                                                     padding = "0 4px", 
                                                     `border-radius` = "4px", 
                                                     `background-color` = ifelse(df$c == 1, 
                                                                                 "transparent",
                                                                                 csscolor(gradient(as.numeric(abs(df$c)), "lightblue", "darkblue")))))

Code

df <- data.frame( a = c(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5),
                  b = 1,
                  c = c(5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5))

formattable(df, 
            list(a = formatter_tile,
                 b = formatter_tile_from_c))

Output enter image description here