R 使用 kableExtra 为单元格着色并使用嵌套 if/ifelse 保持条纹格式?
R Using kableExtra to colorize cells and maintain striped formatting with nested if/ifelse?
这个问题的扩展:
我正在使用 kableExtra 和 cell_spec 使用嵌套的 ifelse 语句为单元格着色。
我不想将小于 .10 的值着色为白色,而是让它们单独放置,以便 kableExtra 应用条纹格式。
我觉得这是不可能的,因为背景颜色是如何应用的?
东风:
DF <- data.frame(V1 = sample(letters,10,T), V2 = abs(rnorm(10)), V3 = abs(rnorm(10)))
代码:
library(magrittr)
library(kableExtra)
paint <- function(x) {
ifelse(x < 0.1, "white", ifelse(x < 0.2, "yellow", "red"))
}
DF[, -1] = lapply(DF[, -1], formatC, format = 'f', flag='0', digits = 2)
DF[,-1] = lapply(DF[,-1], function(x) cell_spec(x, background = paint(x), format = "latex"))
DF %<>%
mutate_if(is.numeric, function(x) {
cell_spec(x, background = paint(x), format = "latex")
})
kable(DF, caption = "colorized table with striping", digits = 2, format = "latex", booktabs = T, escape = F, longtable = T)%>%
kable_styling(latex_options = c("striped", "hold_position", "repeat_header", font_size = 6))%>%
landscape()%>%
row_spec(0, angle = 45)
问题区域?
paint <- function(x) {
ifelse(x < 0.1, "white", ifelse(x < 0.2, "yellow", "red"))
}
这是否可以更改为仅在黄色 (>=.10<.2) 和红色 (>=.2) 之间更改颜色?还是必须定义所有条件?
所需输出:条纹 table 仅突出显示定义的值,允许条纹存在于小于 .10
的值上
您无需对希望保留的单元格应用任何格式。因此,只需在调用 cell_spec
之前测试该条件(即,只为要格式化的单元格调用 cell_spec):
paint <- function(x) ifelse(x < 0.2, "yellow", "red")
DF[,-1] = lapply(DF[,-1], formatC, format = 'f', digits = 2)
DF[,-1] = lapply(DF[,-1], function(x)
ifelse(x < 0.1, x, cell_spec(x, background = paint(x), format = "latex")))
kable(DF, caption = "Highlighted numbers near zero",
digits = 2, format = "latex", booktabs = T, escape = F, longtable = T) %>%
kable_styling(latex_options = c("striped", "hold_position",
"repeat_header", font_size = 6)) %>%
landscape() %>%
row_spec(0, angle = 45)
这个问题的扩展:
我正在使用 kableExtra 和 cell_spec 使用嵌套的 ifelse 语句为单元格着色。
我不想将小于 .10 的值着色为白色,而是让它们单独放置,以便 kableExtra 应用条纹格式。
我觉得这是不可能的,因为背景颜色是如何应用的?
东风:
DF <- data.frame(V1 = sample(letters,10,T), V2 = abs(rnorm(10)), V3 = abs(rnorm(10)))
代码:
library(magrittr)
library(kableExtra)
paint <- function(x) {
ifelse(x < 0.1, "white", ifelse(x < 0.2, "yellow", "red"))
}
DF[, -1] = lapply(DF[, -1], formatC, format = 'f', flag='0', digits = 2)
DF[,-1] = lapply(DF[,-1], function(x) cell_spec(x, background = paint(x), format = "latex"))
DF %<>%
mutate_if(is.numeric, function(x) {
cell_spec(x, background = paint(x), format = "latex")
})
kable(DF, caption = "colorized table with striping", digits = 2, format = "latex", booktabs = T, escape = F, longtable = T)%>%
kable_styling(latex_options = c("striped", "hold_position", "repeat_header", font_size = 6))%>%
landscape()%>%
row_spec(0, angle = 45)
问题区域?
paint <- function(x) {
ifelse(x < 0.1, "white", ifelse(x < 0.2, "yellow", "red"))
}
这是否可以更改为仅在黄色 (>=.10<.2) 和红色 (>=.2) 之间更改颜色?还是必须定义所有条件?
所需输出:条纹 table 仅突出显示定义的值,允许条纹存在于小于 .10
的值上您无需对希望保留的单元格应用任何格式。因此,只需在调用 cell_spec
之前测试该条件(即,只为要格式化的单元格调用 cell_spec):
paint <- function(x) ifelse(x < 0.2, "yellow", "red")
DF[,-1] = lapply(DF[,-1], formatC, format = 'f', digits = 2)
DF[,-1] = lapply(DF[,-1], function(x)
ifelse(x < 0.1, x, cell_spec(x, background = paint(x), format = "latex")))
kable(DF, caption = "Highlighted numbers near zero",
digits = 2, format = "latex", booktabs = T, escape = F, longtable = T) %>%
kable_styling(latex_options = c("striped", "hold_position",
"repeat_header", font_size = 6)) %>%
landscape() %>%
row_spec(0, angle = 45)