To complement what I meant in my comments. Here I'm randomly selecting values to highlight using rbinom(), you just need to manually set whether a particular value should be TRUE or FALSE based on whether that value was imputed or not.
The one tedious aspect is applying the cell_spec() across the columns. I tried to do so dynamically using across(), but I had difficulty doing so. Perhaps someone would find a more functional method.
library(dplyr)
library(knitr)
library(kableExtra)
data("diamonds", package = "ggplot2")
df <- head(diamonds)
df <- df |>
rowwise() |>
mutate(across(color:z, ~ if_else(rbinom(1, 1, 0.5) == 1, TRUE, FALSE), .names = "{.col}_na")) |>
ungroup()
df |>
mutate(color = cell_spec(color, background = if_else(color_na, "red", "white"),
color = if_else(color_na, "white", "black")),
clarity = cell_spec(clarity, background = if_else(clarity_na, "red", "white"),
color = if_else(clarity_na, "white", "black")),
depth = cell_spec(depth, background = if_else(depth_na, "red", "white"),
color = if_else(depth_na, "white", "black")),
table = cell_spec(table, background = if_else(table_na, "red", "white"),
color = if_else(table_na, "white", "black")),
price = cell_spec(price, background = if_else(price_na, "red", "white"),
color = if_else(price_na, "white", "black")),
x = cell_spec(x, background = if_else(x_na, "red", "white"),
color = if_else(x_na, "white", "black")),
y = cell_spec(y, background = if_else(y_na, "red", "white"),
color = if_else(y_na, "white", "black")),
z = cell_spec(z, background = if_else(z_na, "red", "white"),
color = if_else(z_na, "white", "black"))
) |>
select(-ends_with("na")) |>
kable(escape = FALSE) |>
kable_styling()
