I want to highlight cells in a datatable on the condition that the values are contained in another list.
Here is my reproducible example:
library(DT)
library(tidyverse)
df_table <- tibble(
id = 1:10,
V1 = rnorm(10)
)
my_id_list <- c(4, 6, 8)
datatable(df_table)
Now I want to highlight the id
values which are contained in the my_id_list
:
The following code does not work, but should clarify my intention:
datatable(df_table) %>%
formatStyle("id", backgroundColor = if(id %in% my_id_list) {"yellow"})
I am unsure whether a solution can be achieved with the help of R. Probably a solution with javascript makes the most sense, as shown in this issue:
Implementing ifelse (or if_else) in datatable output to conditionally change the background color of a reactive table (shiny and r)