Hi I have a rendered datatable in which I want to conditionally format the Roll_3_Weeks column based off the value in the Target column.
I have tried with styleInterval but I want the formatting to be based off a percentage and the function seems to want whole numbers.
Specifically, I want to color the Roll_3_Weeks cell green if it is within 95% of the Target value*3 and red in all other instances.
An image of my table is below and here is my current code snippet Thanks in advance!
output$backs_week_table <- renderDT({
datatable(Backs_td_week,
extensions = 'FixedColumns',
options = list(
pageLength = 15,
fixedColumns = list(leftColumns = 3),
dom = 't', # Show only the table without search and pagination
columnDefs = list(
list(targets = c(0, 1), className = 'dt-left'), # Left-align the first two columns
list(targets = seq(1, ncol(Backs_td_week) - 1), className = 'dt-center') # Center-align columns starting from the third column
),
rowCallback = JS('function(row, data, index) {
$("td", row).css("padding-top", "5px");
$("td", row).css("padding-bottom", "5px");
}')
),
rownames = FALSE
) %>%
formatStyle(
names(Backs_td_week),
border = '1px solid #ddd' # Add cell borders
) %>%
formatStyle(
names(Backs_td_week)[grepl("Perc_", names(Backs_td_week))],
valueColumns = names(Backs_td_week)[grepl("Perc_", names(Backs_td_week))],
color = 'black',
background = styleColorBar(
sapply(Backs_td_week[, grepl("Perc_", names(Backs_td_week))], function(x) scales::rescale(x, to = c(0, 100))), 'indianred')
)
})

Using DT, you could add a helper
valueColumnwhich, though hidden in the output, decides the style of another column.Example: