Вот способ. Рендеринг не работает в RStudio, но он работает в браузере.
library(DT)
dat <- data.frame(
"Team" = c("Alex", "Beth", "Andrew", "Bert"),
"Group" = c("A","B","A","B"),
"Spending_Q1" = c(1000, 500, 1500, 1000),
"Spending_Q2" = c(500, 2000, 1000, 500),
"Spending_Q3" = c(500, 1000, 1500, 1500)
)
columns <- c(3, 4, 5) # columns we want to colorize
nrows <- nrow(dat)
rowCallback <- c(
"function(row, data, index){",
sprintf(" var columns = [%s];", toString(columns)),
" var min = data[columns[0]];",
" var max = data[columns[0]];",
" for(let i = 1; i < columns.length; i++){",
" min = Math.min(min, data[columns[i]]);",
" max = Math.max(max, data[columns[i]]);",
" }",
sprintf(" var nrows = %d;", nrows),
" var h = index * 360/nrows;",
" for(let i = 0; i < columns.length; i++){",
" var j = columns[i];",
" var l = 75 - 50*(data[j]-min)/(max-min);",
" var color = 'hsl(' + h + ', 100%, ' + l + '%)';",
" $('td:eq(' + j + ')', row).css('background-color', color);",
" }",
"}"
)
datatable(
dat,
options = list(
rowCallback = JS(rowCallback),
columnDefs = list(
list(className = "dt-center", targets = "_all")
)
)
)