简体   繁体   English

条件格式数据表DT R

[英]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. 我是R中DT包的忠实拥护者。我想复制我拥有的一些Excel条件格式的表,但是发现很难访问样式功能。

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. 具体来说,我希望能够创建一个函数,该函数允许用户调用数据表的行/列,并对其应用一些有条件的格式化,这与在excel中的操作类似。 This would be a much added feature to novice R users like myself, and also just speed up the process for everyone else. 对于像我这样的R新手来说,这将是一个增加的功能,并且也会加快其他所有人的处理速度。 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 但我不知道如何应用到各行,但好像你可以通过名字来称呼他们,因为看到这里通过formatStyle()和背景颜色,但你还是不要有阴影渐变,你需要知道的行/列名,有点太多

Any help creating a custom function would be a big help to the R datatable community IMO. 创建自定义函数的任何帮助对于R数据表社区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 以防万一有人碰到这个,这是我写的一个函数,它执行的功能类似于excel菜单

    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 因此, backgroundColor参数采用了JS_EVAL类(仅是JavaScript),对于上述代码(使用示例中设置的断点),其值如下所示

"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 现在,如果有人可以帮助我理解如何更改调色板(例如,从红色过渡到绿色),我认为实现易于使用的功能将不会那么困难,尽管我仍然不确定如何引用一个颜色。列/行旁边使用其名称(尽管似乎有一个参数valueColumns可以调用

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

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