简体   繁体   中英

Conditional Formatting datatable DT R

I am a big fan of the DT package in R. I want to replicate some Excel conditionally formatted tables I have, but finding it difficult to access the styling features.

Specifically, I would love to be able to create a function that allows a user to call a row/column of the datatable and apply some conditional formmating to it, similarly to how it is done in excel. This would be a much added feature to novice R users like myself, and also just speed up the process for everyone else. Unlike a heat map, conditional formatting is important for when row/columns are not all of the same type, therefore you need to do each one individually. Also its nice to be able to speficify what are the high and low value option markers.

I see you can create breaks, as in the follow example on this page

    # create 19 breaks and 20 rgb color values ranging from white to red
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
  {paste0("rgb(255,", ., ",", ., ")")}
datatable(df) %>% formatStyle(names(df), backgroundColor = styleInterval(brks, clrs))

but I am not sure how to apply to individual rows, though it seems like you can call them by name, as seen here via formatStyle() and background color, but then you still dont have the shaded gradient and you need to know the row/column name, which is a bit too much

Any help creating a custom function would be a big help to the R datatable community IMO.

you can try something like this:

 # create 19 breaks and 20 hex color values ranging from red to green using white around the median
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
colfunc <- colorRampPalette(c("red","white","green"))
clrs <- colfunc(length(brks) + 1)
datatable(df) %>% formatStyle(names(df), backgroundColor = styleInterval(brks, clrs))

just in case anyone comes across this, here is a function I wrote that does something similar to the excel menu

    column_cond_format = function(col_max, col_min, pal =c('red','white', 'green')) {
  stop_color_max = col_max ##set as max(column)
  stop_color_min = col_min
  brks <- quantile(c(stop_color_min,stop_color_max), probs = seq(.05, .95, .05), na.rm = TRUE) ## set max/min
  myPal = shades::gradient(pal, 18, space="Lab")
  clrs = c(myPal[1] , myPal, myPal[length(myPal)])

  return(list(brks= brks, clrs = clrs))
}

So the backgroundColor argument take in a JS_EVAL class, which is just JavaScript, and for the above code (using breakpoints set in the example) the values are shown below

"value <= -1.5504 ? 'rgb(255,255,255)' : value <= -0.9689 ? 'rgb(255,244,244)' : value <= -0.7885 ? 'rgb(255,232,232)' : value <= -0.6168 ? 'rgb(255,221,221)' : value <= -0.28425 ? 'rgb(255,210,210)' : value <= -0.1183 ? 'rgb(255,198,198)' : value <= 0 ? 'rgb(255,187,187)' : value <= 0.0754000000000001 ? 'rgb(255,176,176)' : value <= 0.2935 ? 'rgb(255,164,164)' : value <= 0.443 ? 'rgb(255,153,153)' : value <= 0.46745 ? 'rgb(255,142,142)' : value <= 0.5344 ? 'rgb(255,131,131)' : value <= 0.5647 ? 'rgb(255,119,119)' : value <= 0.979 ? 'rgb(255,108,108)' : value <= 1 ? 'rgb(255,97,97)' : value <= 1 ? 'rgb(255,85,85)' : value <= 1.1765 ? 'rgb(255,74,74)' : value <= 1.3743 ? 'rgb(255,63,63)' : value <= 1.65975 ? 'rgb(255,51,51)' : 'rgb(255,40,40)'"

Now if someone could help me understanding how I can change the color pallett (say to transition from Red to Green) I think it wouldnt be so hard to implement an easy to use function, though I am still not sure how to can refer to a column/row beside using its name (though there appears to be an argument valueColumns which you can call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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