简体   繁体   English

R DT rowCallback条件格式化每个单元格

[英]R DT rowCallback conditional formatting each cell

In my DT, I am trying to create conditional formatting for each cell, where formatting is based on different rules for each row. 在我的DT中,我试图为每个单元格创建条件格式,其中格式基于每一行的不同规则。 The goal is to create an easy visual comparison between column 1 and column 2 目的是在第1列和第2列之间创建轻松的视觉比较

For example 例如

If [1,1] < [1,2] highlight [1,1] green & [1,2] red 如果[1,1] <[1,2]高亮显示[1,1]绿色和[1,2]红色

If [2,1] < [2,2] highlight [2,1] red & [2,2] green 如果[2,1] <[2,2]高亮显示[2,1]红色和[2,2]绿色

19 March update : For ease, I built a "winner" column in R, and am trying to compare the data in the table against that column. 3月19日更新 :为方便起见,我在R中建立了一个“优胜者”列,并试图将表中的数据与该列进行比较。

library(DT)

example.data <- data.frame(col.1 = rep(1, 2), 
                       col.2 = rep(2, 2), 
                       winner = c(1,2))

datatable(example.data,
      options = list(
        rowCallback = JS(
          'function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {

                            if (parseFloat(aData[3] = 1)){ 
                              $("td:eq(1)", nRow).css("background-color", "green");
                            } 
                            if (parseFloat(aData[3] = 2)){
                              $("td:eq(1)", nRow).css("background-color", "red");
                            }

                            if (parseFloat(aData[3] = 1)){  
                              $("td:eq(2)", nRow).css("background-color", "red");
                            } 
                            if (parseFloat(aData[3] = 2)) {
                              $("td:eq(2)", nRow).css("background-color", "green");
                            }
                          }'
        )
      ))

The above code turns column 1 red and column 2 green. 上面的代码将第1列变为红色,将第2列变为绿色。 Any help would be appreciated. 任何帮助,将不胜感激。

The errors in the previous code were due to the misuse of the = operator, should have been ==, and passing the argument as a floating point using parseFloat. 先前代码中的错误是由于=运算符的滥用引起的,应该是==,并使用parseFloat将参数作为浮点传递。 For anyone trying to do a similar task, the following example should work. 对于尝试执行类似任务的任何人,下面的示例都应该起作用。

datatable(example.data,
      options = list(
        rowCallback = JS(
          'function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {

                            if (aData[3] == "1"){ 
                              $("td:eq(1)", nRow).css("background-color", "green");
                            } 
                            if (aData[3] == "2"){
                              $("td:eq(1)", nRow).css("background-color", "red");
                            }

                            if (aData[3] == "1"){  
                              $("td:eq(2)", nRow).css("background-color", "red");
                            } 
                            if (aData[3] == "2") {
                              $("td:eq(2)", nRow).css("background-color", "green");
                            }
                          }'
        )
      ))

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

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