Home > Enterprise >  How to colour or highlight a specifics cell?
How to colour or highlight a specifics cell?

Time:09-19

I have a tibble looks like this (here I used snipping tool to select and highlight)

enter image description here

I want to colour the values or highlight the values in cells (1,3), (1,9), (3,3), (4,7), (4,8) and so on. How can I do this in R?

Thank you.

Edition: I tried colouring as

my_df[1,1] = cell_spec(my_df[1,1], color = 'red')
kbl(my_df[1:10,]) %>%
  kable_paper("striped", full_width = F)

but I got this message (it's a long text, I truncated it): <span style=" color: red !important;" >&lt;span style=&quot; color: red !important;&quot; &gt;&amp;lt;span style=&amp;quot;

Where did I go wrong?

CodePudding user response:

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()

enter image description here

CodePudding user response:

You can use extraKable and cell_spec function to define a rule for formatting. See more in the vignette: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html

  • Related