gt table 中多列的条件格式
Conditional formatting of multiple columns in gt table
这是我的第一个 post,如果我搞砸了,请见谅。我正在尝试将条件格式应用于 gt table 的多个列(将样本 SampA、SampB 和 SampB 的结果与 Limit 进行比较)。在 gt examples and a different 之一的带领下,我设法使用以下代码将其应用于单个列(变量):
## Conditional Formatting of single column in gt table
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
gt(samples,rowname_col="Chem") %>% tab_style(
style = list(
cell_fill(color = "grey80"),
cell_text(weight = "bold")
),
locations = cells_body(
columns = vars(SampA),
rows = SampA >= Limit
)
) %>% tab_spanner(
label = "Samples",
columns = vars(SampA,SampB,SampC))
但是,我没有成功地尝试将其扩展到多列。我可以获得与 'vars(SampA,SampB,SampC)' 一起使用的 'columns' 参数。将 'rows' 参数保留为 'SampA >= Limit',格式 'works' 意味着所有 SampA >= Limit 的行都在三个 Samp 列中突出显示,但这不是我想要的. 运行 下面的代码最终没有任何列的格式。
locations = cells_body(
columns = vars(SampA,SampB,SampC),
rows = vars(SampA,SampB,SampC) >= Limit
)
我已经能够通过重复为每一列量身定制的 'style_tab()' 来“暴力破解”我想要的东西,但我知道必须有更好的方法来实现我的目标。帮忙?
library(gt)
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
samples <- samples %>%
mutate(Limit = as.numeric(Limit))
gt(samples,
rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampA),
rows = SampA >= Limit)) %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampB),
rows = SampB >= Limit)) %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampC),
rows = SampC >= Limit)) %>%
tab_spanner(
label = "Samples",
columns = vars(SampA,SampB,SampC))
我知道这可能为时已晚,无法为您提供帮助,但我能够通过定义一个函数来解决这个问题,该函数使用正确的参数创建一个 cells_body 调用列表。然后将此列表传递给 locations 参数并将指定的格式应用于所有选定的单元格。希望其他人觉得这很有用!
library(gt)
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
builder <- function(x, Limit){cells_body(columns = !!sym(x), rows = !!sym(x) > Limit)}
gt(samples,rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = "grey80"), cell_text(weight = "bold")),
locations = lapply(c("SampA", "SampB", "SampC"), test_fun, Limit = sym(Limit))) %>%
tab_spanner(label = "Samples", columns = c(SampA, SampB, SampC))
这将适用于与您的列名称匹配的任何字符串向量,包括可以通过 colnames()
函数创建的内容
names <- colnames(samples)[3:ncol(samples)]
gt(samples,rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = "grey80"), cell_text(weight = "bold")),
locations = lapply(names, builder, Limit = sym(Limit))) %>%
tab_spanner(label = "Samples", columns = c(SampA, SampB, SampC))
这是我的第一个 post,如果我搞砸了,请见谅。我正在尝试将条件格式应用于 gt table 的多个列(将样本 SampA、SampB 和 SampB 的结果与 Limit 进行比较)。在 gt examples and a different
## Conditional Formatting of single column in gt table
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
gt(samples,rowname_col="Chem") %>% tab_style(
style = list(
cell_fill(color = "grey80"),
cell_text(weight = "bold")
),
locations = cells_body(
columns = vars(SampA),
rows = SampA >= Limit
)
) %>% tab_spanner(
label = "Samples",
columns = vars(SampA,SampB,SampC))
但是,我没有成功地尝试将其扩展到多列。我可以获得与 'vars(SampA,SampB,SampC)' 一起使用的 'columns' 参数。将 'rows' 参数保留为 'SampA >= Limit',格式 'works' 意味着所有 SampA >= Limit 的行都在三个 Samp 列中突出显示,但这不是我想要的. 运行 下面的代码最终没有任何列的格式。
locations = cells_body(
columns = vars(SampA,SampB,SampC),
rows = vars(SampA,SampB,SampC) >= Limit
)
我已经能够通过重复为每一列量身定制的 'style_tab()' 来“暴力破解”我想要的东西,但我知道必须有更好的方法来实现我的目标。帮忙?
library(gt)
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
samples <- samples %>%
mutate(Limit = as.numeric(Limit))
gt(samples,
rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampA),
rows = SampA >= Limit)) %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampB),
rows = SampB >= Limit)) %>%
tab_style(style = list(cell_fill(color = 'yellow'),
cell_text(weight = 'bold')),
locations = cells_body(columns=vars(SampC),
rows = SampC >= Limit)) %>%
tab_spanner(
label = "Samples",
columns = vars(SampA,SampB,SampC))
我知道这可能为时已晚,无法为您提供帮助,但我能够通过定义一个函数来解决这个问题,该函数使用正确的参数创建一个 cells_body 调用列表。然后将此列表传递给 locations 参数并将指定的格式应用于所有选定的单元格。希望其他人觉得这很有用!
library(gt)
samples = as_tibble(cbind("Chem"=c("Cd","Pb","Zn"),
"Limit"=c("0.005","0.05","0.007"),
"SampA" = c("0.001","0.15","0.003"),
"SampB" = c("0.002","0.04","0.005"),
"SampC" = c("0.009","0.23","0.03")))
builder <- function(x, Limit){cells_body(columns = !!sym(x), rows = !!sym(x) > Limit)}
gt(samples,rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = "grey80"), cell_text(weight = "bold")),
locations = lapply(c("SampA", "SampB", "SampC"), test_fun, Limit = sym(Limit))) %>%
tab_spanner(label = "Samples", columns = c(SampA, SampB, SampC))
这将适用于与您的列名称匹配的任何字符串向量,包括可以通过 colnames()
函数创建的内容
names <- colnames(samples)[3:ncol(samples)]
gt(samples,rowname_col="Chem") %>%
tab_style(style = list(cell_fill(color = "grey80"), cell_text(weight = "bold")),
locations = lapply(names, builder, Limit = sym(Limit))) %>%
tab_spanner(label = "Samples", columns = c(SampA, SampB, SampC))