Coloring DT columns

Hi all,

Is there a way to color "Emails" column below (Red if "Emails" is greater than "Calls", or else, its yellow). The challenge here is , can we also hide "Calls" column as well.

library(shiny)
library(tidyverse)
library(DT)

x <- tibble(
  Unit = c("Sales", "Marketing", "HR"), 
  Calls = c(100, 150, 120), 
  Emails = c(200, 220, 230), 
  Calls_goal = c(1, 0, 0), 
  Emails_goal = c(0, 1, 1)
)


ui <- fluidPage(
  
  mainPanel(
    DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  
  output$table <- DT::renderDataTable({
    
    # Can't use both visible = FALSE and rownames = FALSE
    
    datatable(x,
              rownames = TRUE)
    
  })
}

shinyApp(ui = ui, server = server)
library(shiny)
library(tidyverse)
library(DT)
library(glue)

x <- tibble(
  Unit = c("Sales", "Marketing", "HR", "HR2"),
  Calls = c(100, 150, 120, 300),
  Emails = c(200, 220, 230, 200)
)


ui <- fluidPage(
  mainPanel(
    DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  output$table <- DT::renderDataTable({
    
    px_1 <- x %>% mutate(
      e_ge_c = Emails >= Calls,
      Emails_HTML = case_when(
        e_ge_c ~ glue("<span style='color:red;'>{Emails}</span>"),
        TRUE ~ glue("<span style='color:green;'>{Emails}</span>")
      )
    )
    print(px_1)
    px_2 <- px_1 %>%
      select(Unit, Emails_HTML) %>%
      rename(Emails = Emails_HTML)
    print(px_2)
    datatable(px_2,
      rownames = TRUE,
      escape = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.