我在工作中经常使用{gt}包;然而,我必须将多种 colored颜色 应用于几个报告中的多个列.因此,我需要您的帮助来使用{Purrr}包自动执行此操作.下面是一个类似于我每月工作的数据集的示例.
感谢您的好意帮助!
data_tbl <- tibble(
name = c('deng', 'alier', 'atem', 'garang', 'akuien'),
english = c(87, 57, 76, 98, 79),
mathematics = c(88, 98, 87, 69, 88),
statistics = c(76, 99, 58, 84, 90)
)
# Tabulate the data with gt
my_gt <- data_tbl %>%
# Initialize a gt table
gt() %>%
# Formatting
tab_style(
style = list(
cell_fill(color = "#1dab48")
# cell_text(color = "white")
),
locations = cells_body(
columns = english,
rows = english >= 80
)
) %>%
tab_style(
style = list(
cell_fill(color = "#1dab48")
# cell_text(color = "white")
),
locations = cells_body(
columns = mathematics,
rows = mathematics >= 80
)
) %>%
tab_style(
style = list(
cell_fill(color = "#1dab48")
# cell_text(color = "white")
),
locations = cells_body(
columns = statistics,
rows = statistics >= 80
)
) %>%
# Formatting - yellow
tab_style(
style = list(
cell_fill(color = "#c97928")
# cell_text(color = "white")
),
locations = cells_body(
columns = english,
rows = english >= 70 & english < 80
)
) %>%
tab_style(
style = list(
cell_fill(color = "#c97928")
# cell_text(color = "white")
),
locations = cells_body(
columns = mathematics,
rows = mathematics >= 70 & mathematics < 80
)
) %>%
tab_style(
style = list(
cell_fill(color = "#c97928")
# cell_text(color = "white")
),
locations = cells_body(
columns = statistics,
rows = statistics >= 70 & statistics < 80
)
) %>%
# Formatting - red
tab_style(
style = list(
cell_fill(color = "#c94e28")
# cell_text(color = "white")
),
locations = cells_body(
columns = english,
rows = english < 70
)
) %>%
tab_style(
style = list(
cell_fill(color = "#c94e28")
# cell_text(color = "white")
),
locations = cells_body(
columns = mathematics,
rows = mathematics < 70
)
) %>%
tab_style(
style = list(
cell_fill(color = "#c94e28")
# cell_text(color = "white")
),
locations = cells_body(
columns = statistics,
rows = statistics < 70
)
)