-->

Use multiple conditions and formats on datatable R

2019-08-15 02:28发布

问题:

Conditional Formatting datatable R shiny (multiple conditions and formats)

The question how to highlight the max on each row is answered here: Conditional formatting in DT Data Table R Shiny

I want to highlight not only the maximum of each column in green, and the minimum in red, but also make those cells > median of that column appear in bold. How do I make that work?

The below code is from the linked question, adapted for my needs. But unfortunately not working.

library(shinydashboard)
library(DT)
library(magrittr)

entity <- c('entity1', 'entity2', 'entity3')
value1 <- c(21000, 23400, 26800)
value2 <- c(21234, 23445, 26834)
value3 <- c(21123, 234789, 26811)
value4 <- c(27000, 23400, 26811)
entity.data <- data.frame(entity, value1, value2, value3, value4)

# Create a vector of max values
max_val <- apply(entity.data[, -1], 2, max)
# Create a vector of min values
min_val <- apply(entity.data[, -1], 2, min)
# Create a vector of median values
median_val <- apply(entity.data[, -1], 2, median)

header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(DT::dataTableOutput("entity.dataTable"))

shinyApp(
  ui = dashboardPage(header, sidebar, body),
  server = function(input, output) {
    output$entity.dataTable <- renderDataTable({
      DT::datatable(
        entity.data,
        rownames = FALSE,
      ) %>% 
        formatStyle(columns = 2:5,
                   backgroundColor = styleEqual(levels = max_val, values = rep("green", length(max_val))))%>%
        formatStyle(columns = 2:5,
                    backgroundColor = styleEqual(levels = min_val, values = rep("red", length(min_val))))
    })
  }
)

回答1:

since you have separate values for each column you have to asign the values separately for each column

shinyApp(
  ui = dashboardPage(header, sidebar, body),
  server = function(input, output) {
    output$entity.dataTable <- renderDataTable({
      DT::datatable(
        entity.data,
        rownames = FALSE,
      ) %>% 
        formatStyle(columns = 2,
                    backgroundColor = styleInterval(cuts =c(min_val[1],max_val[1]-1), values = c("#F00","none","#00F")),
                    fontWeight = styleInterval(cuts= median_val[1]-1,values = c("normal","bold"))) %>% 
        formatStyle(columns = 3,
                    backgroundColor = styleInterval(cuts =c(min_val[2],max_val[2]-1), values = c("#F00","none","#00F")),
                    fontWeight = styleInterval(cuts= median_val[2]-1,values = c("normal","bold")))%>% 
        formatStyle(columns = 4,
                    backgroundColor = styleInterval(cuts =c(min_val[3],max_val[3]-1), values = c("#F00","none","#00F")),
                    fontWeight = styleInterval(cuts= median_val[3]-1,values = c("normal","bold")))%>% 
        formatStyle(columns = 5,
                    backgroundColor = styleInterval(cuts =c(min_val[4],max_val[4]-1), values = c("#F00","none","#00F")),
                    fontWeight = styleInterval(cuts= median_val[4]-1,values = c("normal","bold"))) 

    })
  }
)

Hope this helps!!



标签: r shiny dt