如何在具有相同值的弹性相等单元格中着色

问题描述 投票:0回答:1
我有这个数据集

A B 120 NHH uJc 59 NHH GBY 30 NHH rYv 84 NHH hYl 77 NHH hat 101 NHH hqL 167 NHH jxF 1011 NHH hqL 51 NHH VMg 53 cUU gWX 82 cUU Vae 1201 cUU uJc 591 cUU GBY 821 cUU Vae 841 cUU hYl 1202 cUU uJc 48 cUU pEI 531 cUU gWX 76 cUU ZCY 761 cUU ZCY 822 cUU Vae 823 cUU Vae 70 cUU rYQ 96 cUU veT 1012 cUU hqL 824 cUU Vae 842 cUU hYl 72 cUU QIN
我想创建一个灵活的单元格,以便具有相同值的单元格以相同的方式着色。为了做到这一点,我使用了以下代码:

library(digest) library(flextable) library(tidyverse) unique_flows <- unique(ft$flow) hash_colors <- sapply(unique_flows, function(x) color_set[as.integer(digest(x, algo="crc32", serialize=FALSE)) %% length(color_set) + 1]) color_map <- setNames(hash_colors, unique_flows) ft %>% flextable() %>% bg(i = 1:nrow(ft), j = "flow", bg = ft$color, part = "body") which ends upp wwith an error Error in x$data[i, j] <- value : number of items to replace is not a multiple of replacement length
任何人都可以帮我解决这个问题吗?谢谢

r colors cell flextable
1个回答
0
投票
如果没有可重现的示例,就很难提供帮助。我只会使用您的数据和第一列来演示:

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)

© www.soinside.com 2019 - 2024. All rights reserved.