Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Changing cell color of R shiny data table dependent on its value

I am trying to color cells that meet certain conditions. For example, I would like to color cells whose value is equal to its respective column values of the first row of the data. I figured how to color specific values using javascript but can't figure out how to set the conditions as what I want. Here's the code:

library(DT)
df <- head(iris)
df %>% 
  datatable %>% 
  formatStyle(1:2, color = JS("value % 1 === 0 ? 'red' : ''"))

In this table, the highlighted cells are the ones whose values match the value of the column of the first row of the data. I would like to set javascript conditions as such that it will highlight the cells as shown in the image.

example

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you use styleEqual function, it almost work except that it underlines also the reference row :

library(DT)
df <- head(iris)
ref <- df[1,]
color <- "orange"
datatable(df) %>% 
    formatStyle(1:5,
                backgroundColor = styleEqual(ref[1:5], rep(color, 5)))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!