How to style one column based on the values of another, using DT?

We can style one column based on the values from another using the following code from this link
https://rstudio.github.io/DT/010-style.html

# style V1 base on values of V6
datatable(df) %>% formatStyle(
  'V1', 'V6',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

How can we adapt the code to display
V1 based V6 values
V2 based on V5 values
V3 based on V4 values
all simultaneously

Also, if possible, before display, I would like to delete V4,V5 and V6 columns.
So, there will be V1:V3 columns with color coding from V4:V6

I tried

# style V1:V3 base on values of V4:V6
datatable(df) %>% formatStyle(
 'V1', 'V6','V2', 'V5','V3', 'V4',
 backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

Answer found :slight_smile:

datatable(df) %>% formatStyle(
 'V1', 'V6',
 backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
) %>% formatStyle(
 'V2', 'V5',
 backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
) %>% formatStyle(
 'V3', 'V4',
 backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

In order to remove unnecessary columns:

datatable(df, options = list(
    columnDefs = list(list(targets = c(4,5,6), visible = FALSE))
)) %>%

This topic was automatically closed 7 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.