Color cell block color in DT table

Hi all,

Is there a way to color only cell block in DTtable cells. For example the below code will color both cell background and font . But can we color only block where the number resides.

datatable(iris, options = list(pageLength = 5)) %>%
  formatStyle('Sepal.Length',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')

Another reprex for reference. This application colors only font. But can we color a only a block of it (Not entire background)

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

shinyApp(
  ui = fluidPage(
    
    sidebarLayout(
      sidebarPanel(
        fluidRow(  
          column(8,dataTableOutput("view"))
        )
      ),
      
      mainPanel(
      ))),
  
  server = function(input, output, session){
    
    correct <- reactiveValues(num = 7)
    wrong <- reactiveValues(num = 4)   
    skipped <- reactiveValues(num = 9)
    
    togo = 80
    
    output$view <- renderDataTable(datatable(tibble(
      Right = correct$num,
      Wrong = wrong$num,
      Skipped = skipped$num,
      ToGo = togo
    ), options = list(dom = 't')) %>% formatStyle("Right",color=styleEqual(7, "red")) ) 
  }
)

my solution using formattable

library(shiny)
library(tidyverse)
library(DT)
library(formattable)
shinyApp(
  ui = fluidPage(
    
    sidebarLayout(
      sidebarPanel(
        fluidRow(  
          column(8,dataTableOutput("view"))
        )
      ),
      
      mainPanel(
      ))),
  
  server = function(input, output, session){
    
    correct <- reactiveValues(num = 7)
    wrong <- reactiveValues(num = 4)   
    skipped <- reactiveValues(num = 9)
    
    togo = 80
        
    output$view <- renderDataTable(as.datatable(tibble(
      Right = correct$num,
      Wrong = wrong$num,
      Skipped = skipped$num,
      ToGo = togo
    ) %>% formattable(
      list(area(col=Right)~formatter(.tag = "span",
                                     style = x ~ style("background-color" = "gray",
                                                       "color" = ifelse(x==7,"red","black"))))
    ), options = list(dom = 't')) ) 
  }
)

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.