gpt4 book ai didi

r - 用 gt 中的条件信息给单元格着色

转载 作者:行者123 更新时间:2023-12-05 06:56:32 24 4
gpt4 key购买 nike

我正在尝试使用 gt 包为每行中具有最大值的单元格着色(即为每种类型的最大值着色)。以下失败:

df <- tibble(Type=c("a","b","c"),V2=c(7:9), V3 = c(9,8,1), V4 = c(12,31,105))
df %>%
gt() %>%
tab_style(
style = list(
cell_fill(color = "#D9654B")
),
locations = cells_body(
columns = vars(V2, V3, V4), # not needed if coloring all columns
rows = max(.))
)

请提供想法。

最佳答案

您可以通过添加三个单独的 tab_style() 调用来完成此操作

library(gt)
library(tidyverse)

df <- tibble(Type=c("a","b","c"),V2=c(7:9), V3 = c(9,8,1), V4 = c(12,31,105))

df %>% gt() %>%
tab_style(
style = list(
cell_fill(color = "#D9654B")
) ,
locations = cells_body(
columns = vars(V2),
rows = V2 == max(V2))
) %>%
tab_style(
style = list(
cell_fill(color = "#D9654B")
) ,
locations = cells_body(
columns = vars(V3),
rows = V3 == max(V3))
) %>%
tab_style(
style = list(
cell_fill(color = "#D9654B")
) ,
locations = cells_body(
columns = vars(V4),
rows = V4 == max(V4))
)

example

关于r - 用 gt 中的条件信息给单元格着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65103680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com