How to color the background of a cell in datatable (DT package) in R with column and row names or indices?

后端 未结 1 931
再見小時候
再見小時候 2021-01-15 18:21

Here is an example. I created a data frame and use that to create a datatable for visualization. As you can see, my column name and the row from the first column indicate co

相关标签:
1条回答
  • 2021-01-15 18:42

    I'm not sure to get the question, but if you want to change the background color of a cell given by its row index and its column index (that's what I understand), you can do:

    changeCellColor <- function(row, col){
      c(
        "function(row, data, num, index){",
        sprintf("  if(index == %d){", row-1),
        sprintf("    $('td:eq(' + %d + ')', row)", col),
        "    .css({'background-color': 'orange'});",
        "  }",
        "}"  
      )
    }
    datatable(dat, 
              options = list(
                dom = "t",
                rowCallback = JS(changeCellColor(1, 2))
              )
    )
    
    0 讨论(0)
提交回复
热议问题