I have written a reproducible code but the colours do not change
library(shiny)
library(DT)
# define the UI
ui <- fluidPage(
DTOutput("pairwise_table")
)
# define the server
server <- function(input, output) {
# create a sample pairwise comparison dataframe
pairwise_comp <- data.frame(
A = c(NA, "EXTREMELY LOW", "MODERATE", "MODERATE"),
B = c(NA, NA, "EXTREMELY LOW", "EXTREMELY LOW"),
C = c(NA, NA, NA, "VERY LOW"),
D = c(NA, NA, NA, NA)
)
row.names(pairwise_comp)<-c("A","B","C","D")
# define a function to color the cells based on their values
color_cells <- function(x) {
color <- ifelse(is.na(x), "white",
ifelse(x == "EXTREMELY LOW", "red",
ifelse(x == "VERY LOW", "orange", "green")))
c("background-color" = color)
}
# display the pairwise comparison dataframe as an interactive data table
output$pairwise_table <- renderDT({
datatable(pairwise_comp,
options = list(
columnDefs = list(
list(targets = "_all", className = "dt-center")
)
),
extensions = list(
list(
name = 'rowCallback',
js = JS("
function(row, data, index) {
$('td', row).css(color_cells(data));
}
")
)
)
)
})
}
# run the Shiny app
shinyApp(ui, server)