kable 表中的条件格式
我遵循了来自 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一缺少的是在
kbl
中指定escape = FALSE
。适用于iris
数据的示例,因为您没有提供原始数据集“ontrack_lanes”The only thing missing is to specify
escape = FALSE
inkbl
. Example adapted foriris
data, since you did not provide the original dataset "ontrack_lanes"