使用 ifelse() 条件在 KableExtra 中创建 cell_spec() 工具提示
Using ifelse() conditionals to create cell_spec() tooltips in KableExtra
我想使用条件为 Kable 中的某些单元格添加特定的工具提示。我在下面的示例中尝试了以下内容:
col1 <- c("A", "A*","B**")
col2 <- c("A**", "B", "C")
col3 <- c("A*", "B*", "C*")
Test <- data.frame(col1,col2,col3)
Test
Test %>%
mutate_at(vars("col1":"col3"), ~ cell_spec(
., "html",
tooltip = ifelse(. =="A*"|.=="B*"|.=="C*"|.=="D*", "Satisfactory to 22\u00B0C",
ifelse(. == "A**"|.=="B**"|.=="C**"|.=="D**","Satisfactory to 48\u00B0C", )))) %>%
kable(format = "html", escape = FALSE) %>%
kable_styling(full_width = FALSE,
bootstrap_options = c("striped","responsive", "hover"))
我想为每个带有一个星号(例如 A*、B*、C*)和“48\u00B0C 满意”的观察结果添加工具提示“22\u00B0C 满意”每个观察值都带有两个星号(A**、B**、C**)。我也想单独留下其他数据。目前,如果我在 ifelse 语句末尾包含所有 FALSE 观察的工具提示,我只能让它工作。我尝试将“else”参数设置为 NULL,但这没有用。任何帮助将不胜感激,因为我对条件语句很生疏。
当然,你可以只使用一个空字符串""
library(kableExtra, include.only = NULL)
library(dplyr, include.only = "%>%")
col1 <- c("A", "A*","B**")
col2 <- c("A**", "B", "C")
col3 <- c("A*", "B*", "C*")
Test <- data.frame(col1,col2,col3)
Test
#> col1 col2 col3
#> 1 A A** A*
#> 2 A* B B*
#> 3 B** C C*
Test %>%
dplyr::mutate_at(
.vars = dplyr::vars("col1":"col3"),
.funs = ~kableExtra::cell_spec(
x = .,
format = "html",
tooltip = ifelse(test = . =="A*"|.=="B*"|.=="C*"|.=="D*",
yes = "Satisfactory to 22\u00B0C",
no = ifelse(test = . == "A**"|.=="B**"|.=="C**"|.=="D**",
yes = "Satisfactory to 48\u00B0C",
no = "")))) %>%
kableExtra::kable(format = "html", escape = FALSE) %>%
kableExtra::kable_styling(full_width = FALSE,
bootstrap_options = c("striped","responsive", "hover"))
我想使用条件为 Kable 中的某些单元格添加特定的工具提示。我在下面的示例中尝试了以下内容:
col1 <- c("A", "A*","B**")
col2 <- c("A**", "B", "C")
col3 <- c("A*", "B*", "C*")
Test <- data.frame(col1,col2,col3)
Test
Test %>%
mutate_at(vars("col1":"col3"), ~ cell_spec(
., "html",
tooltip = ifelse(. =="A*"|.=="B*"|.=="C*"|.=="D*", "Satisfactory to 22\u00B0C",
ifelse(. == "A**"|.=="B**"|.=="C**"|.=="D**","Satisfactory to 48\u00B0C", )))) %>%
kable(format = "html", escape = FALSE) %>%
kable_styling(full_width = FALSE,
bootstrap_options = c("striped","responsive", "hover"))
我想为每个带有一个星号(例如 A*、B*、C*)和“48\u00B0C 满意”的观察结果添加工具提示“22\u00B0C 满意”每个观察值都带有两个星号(A**、B**、C**)。我也想单独留下其他数据。目前,如果我在 ifelse 语句末尾包含所有 FALSE 观察的工具提示,我只能让它工作。我尝试将“else”参数设置为 NULL,但这没有用。任何帮助将不胜感激,因为我对条件语句很生疏。
当然,你可以只使用一个空字符串""
library(kableExtra, include.only = NULL)
library(dplyr, include.only = "%>%")
col1 <- c("A", "A*","B**")
col2 <- c("A**", "B", "C")
col3 <- c("A*", "B*", "C*")
Test <- data.frame(col1,col2,col3)
Test
#> col1 col2 col3
#> 1 A A** A*
#> 2 A* B B*
#> 3 B** C C*
Test %>%
dplyr::mutate_at(
.vars = dplyr::vars("col1":"col3"),
.funs = ~kableExtra::cell_spec(
x = .,
format = "html",
tooltip = ifelse(test = . =="A*"|.=="B*"|.=="C*"|.=="D*",
yes = "Satisfactory to 22\u00B0C",
no = ifelse(test = . == "A**"|.=="B**"|.=="C**"|.=="D**",
yes = "Satisfactory to 48\u00B0C",
no = "")))) %>%
kableExtra::kable(format = "html", escape = FALSE) %>%
kableExtra::kable_styling(full_width = FALSE,
bootstrap_options = c("striped","responsive", "hover"))