rcolorscellflextable

How to color in flextable with an as_grouped_data format with equal cells with same values


I have this dataset and I am trying coloring with the same color,cells with same values, after using as_grouped_data(., groups = "") function.

dat <- data.frame(
  stringsAsFactors = FALSE,
  A = c("NHH","NHH","NHH","NHH",
        "NHH","NHH","NHH","NHH","NHH","cUU","cUU","cUU",
        "cUU","cUU","cUU","cUU","cUU","cUU","cUU","cUU","cUU",
        "cUU","cUU","cUU","cUU","cUU","cUU","cUU"),
  B = c("uJc","GBY","rYv","hYl",
        "hat","hqL","jxF","hqL","VMg","gWX","Vae","uJc",
        "GBY","Vae","hYl","uJc","pEI","gWX","ZCY","ZCY","Vae",
        "Vae","rYQ","veT","hqL","Vae","hYl","QIN")
)%>% as_grouped_data(., groups = "A")


cid <- dplyr::consecutive_id(dat$B)
cols <- sample(colors(), size = length(unique(cid)))
mycolors <- cols[cid]


flextable(dat) |> 
  bg(i = 1:nrow(dat), j = 'B', bg = mycolors, part = 'body')

I am noticing that color generated like are randonly chosen and thus there some like black and similar which are too dark to color the cells with. Is it possible to fix this issue somehow?


Solution

  • Without a reproducible example, it's harder to help. I will only use your data and the first column to demo:

    dat <- data.frame(
      stringsAsFactors = FALSE,
                     A = c("NHH","NHH","NHH","NHH",
                           "NHH","NHH","NHH","NHH","NHH","cUU","cUU","cUU",
                           "cUU","cUU","cUU","cUU","cUU","cUU","cUU","cUU","cUU",
                           "cUU","cUU","cUU","cUU","cUU","cUU","cUU"),
                     B = c("uJc","GBY","rYv","hYl",
                           "hat","hqL","jxF","hqL","VMg","gWX","Vae","uJc",
                           "GBY","Vae","hYl","uJc","pEI","gWX","ZCY","ZCY","Vae",
                           "Vae","rYQ","veT","hqL","Vae","hYl","QIN")
    )
    
    cid <- dplyr::consecutive_id(dat$A)
    cols <- sample(colors(), size = length(unique(cid)))
    mycolors <- cols[cid]
      
      
    flextable(dat) |> 
      bg(bg = mycolors)
    

    enter image description here

    To answer your updated question, here is a code. Note as_grouped_data() output should be used with as_flextable(), not flextable(). Here, there is one single color that highlight repeated cells.

    library(tidyverse)
    library(flextable)
    dat <- data.frame(
      stringsAsFactors = FALSE,
      A = c("NHH","NHH","NHH","NHH",
            "NHH","NHH","NHH","NHH","NHH","cUU","cUU","cUU",
            "cUU","cUU","cUU","cUU","cUU","cUU","cUU","cUU","cUU",
            "cUU","cUU","cUU","cUU","cUU","cUU","cUU"),
      B = c("uJc","GBY","rYv","hYl",
            "hat","hqL","jxF","hqL","VMg","gWX","Vae","uJc",
            "GBY","Vae","hYl","uJc","pEI","gWX","ZCY","ZCY","Vae",
            "Vae","rYQ","veT","hqL","Vae","hYl","QIN")
    ) %>% as_grouped_data(., groups = "A")
    z <- dat |> 
      mutate(
        cid = case_when(
          !is.na(B) ~ consecutive_id(B),
          TRUE ~ NA
        )
      ) |> add_count(cid)
    
    
    as_flextable(dat) |> 
      bg(i = ~!is.na(B) & z$n > 1, j = 'B', bg = "yellow", part = 'body')