kable 表中的条件格式

发布于 2025-01-11 09:14:00 字数 1852 浏览 3 评论 0原文

我遵循了来自 RStudio 社区

我没有格式化列,而是在表格中看到了 html 代码。

on_track_grades <- on_track_lanes %>%
  group_by(id_number) %>%
  mutate(grades = sum(mark %in% c("D", "F"))) %>%
  pivot_wider(names_from = department, values_from = mark) %>%
  mutate(percentage_present = 100 - as.numeric(percentage_absent)) %>%
  ungroup() %>%
  mutate(lanes = case_when(
    grades == 0 & cum_gpa > 3.5 ~ "Competitive",
    grades == 0 & cum_gpa >= 3.0 & cum_gpa <= 3.5 & percentage_present >= 80 ~ "Promising",
    grades <= 1 & cum_gpa >= 2.0 & cum_gpa <= 2.99 & percentage_present >= 70 ~ "Potential",
    grades <= 3 & cum_gpa >= 1.0 & cum_gpa <= 1.99 & percentage_present >= 50 ~ "Vulnerable",
    cum_gpa <= .99  ~ "Highly Vulnerable",
    TRUE ~ NA_character_
  )) %>% select(school_name, id_number, grades, cum_gpa, percentage_present, lanes) %>%
  drop_na() %>%
  mutate(lanes = cell_spec(lanes, background = case_when(
    lanes == "Competitive" ~  "#005488",
    lanes == "Promising" ~ "#715da6",
    lanes == "Potential" ~ "#c65da3",
    lanes == "Vulnerable" ~ "#ff6881",
    lanes == "Highly Vulnerable" ~ "#ff934f")))

on_track_grades %>%
  filter(school_name == params$school_name) %>%
  kbl(booktabs = TRUE,
      col.names = c("School", "Student ID", "# Ds & Fs", "GPA", "Present", "On-Track Lane"),
      align = c("l", "l", "r", "r", "r", "l"),
      linesep = "") %>%
  kable_paper(full_width = TRUE)

这是我得到的输出... 输入图片此处描述

I followed a conditional formatting script for Kable from RStudio Community

Instead of formatting the column, I see the html code in my table.

on_track_grades <- on_track_lanes %>%
  group_by(id_number) %>%
  mutate(grades = sum(mark %in% c("D", "F"))) %>%
  pivot_wider(names_from = department, values_from = mark) %>%
  mutate(percentage_present = 100 - as.numeric(percentage_absent)) %>%
  ungroup() %>%
  mutate(lanes = case_when(
    grades == 0 & cum_gpa > 3.5 ~ "Competitive",
    grades == 0 & cum_gpa >= 3.0 & cum_gpa <= 3.5 & percentage_present >= 80 ~ "Promising",
    grades <= 1 & cum_gpa >= 2.0 & cum_gpa <= 2.99 & percentage_present >= 70 ~ "Potential",
    grades <= 3 & cum_gpa >= 1.0 & cum_gpa <= 1.99 & percentage_present >= 50 ~ "Vulnerable",
    cum_gpa <= .99  ~ "Highly Vulnerable",
    TRUE ~ NA_character_
  )) %>% select(school_name, id_number, grades, cum_gpa, percentage_present, lanes) %>%
  drop_na() %>%
  mutate(lanes = cell_spec(lanes, background = case_when(
    lanes == "Competitive" ~  "#005488",
    lanes == "Promising" ~ "#715da6",
    lanes == "Potential" ~ "#c65da3",
    lanes == "Vulnerable" ~ "#ff6881",
    lanes == "Highly Vulnerable" ~ "#ff934f")))

on_track_grades %>%
  filter(school_name == params$school_name) %>%
  kbl(booktabs = TRUE,
      col.names = c("School", "Student ID", "# Ds & Fs", "GPA", "Present", "On-Track Lane"),
      align = c("l", "l", "r", "r", "r", "l"),
      linesep = "") %>%
  kable_paper(full_width = TRUE)

This is the ouput I get...
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2025-01-18 09:14:00

唯一缺少的是在 kbl 中指定 escape = FALSE。适用于 iris 数据的示例,因为您没有提供原始数据集“ontrack_lanes”

set.seed("12")
iris[sample(1:nrow(iris), 12),] %>%
  dplyr::select(Species, Sepal.Length, Sepal.Width) %>%
  mutate(Species = cell_spec(Species, background = case_when(
    Species == "setosa" ~  "#005488",
    Species == "versicolor" ~ "#715da6",
    Species == "virginica" ~ "#c65da3")))  %>%
  kbl(booktabs = TRUE,
      col.names = c("Species", "Sepal.Length", "Sepal.Width"),
      align = c("l", "r", "r"),
      linesep = "",
      escape = FALSE) %>%
  kable_paper(full_width = TRUE)

在此处输入图像描述

The only thing missing is to specify escape = FALSE in kbl. Example adapted for iris data, since you did not provide the original dataset "ontrack_lanes"

set.seed("12")
iris[sample(1:nrow(iris), 12),] %>%
  dplyr::select(Species, Sepal.Length, Sepal.Width) %>%
  mutate(Species = cell_spec(Species, background = case_when(
    Species == "setosa" ~  "#005488",
    Species == "versicolor" ~ "#715da6",
    Species == "virginica" ~ "#c65da3")))  %>%
  kbl(booktabs = TRUE,
      col.names = c("Species", "Sepal.Length", "Sepal.Width"),
      align = c("l", "r", "r"),
      linesep = "",
      escape = FALSE) %>%
  kable_paper(full_width = TRUE)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文