将 gt::html(glue::glue(()) 应用于具有 NA 值的列
Apply gt::html(glue::glue(()) to columns with NA values
对于数据集df
如下:
df <- structure(list(type = c("v1", "v2", "v3"), value = c(1.2, 1.6,
-0.3), direction1 = c(0.1, NA, 0), direction2 = c(0.0, -0.1, 1.2
)), class = "data.frame", row.names = c(NA, -3L))
使用下面的代码,我可以通过条件着色和添加 arrow
s:
来自定义列 direction2
df %>%
gt() %>%
fmt_missing(
columns = c('direction1', 'direction2'),
missing_text = "-"
) %>%
text_transform(
locations = cells_body(columns = c(`direction2`)),
fn = function(x){
`direction1` <- as.numeric(x)
`direction2` <- as.numeric(x)
choose_logo <-function(x){
if (x == 0){
gt::html(glue::glue("<span style='color:#bfbfbf;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-right", fill = gray))
} else if (x > 0){
gt::html(glue::glue("<span style='color:#CF000F;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-up", fill = red))
} else if (x < 0) {
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-down", fill = blue))
}
else if (x == '-') {
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("", fill = tjd_blue))
}
}
map(`direction1`, choose_logo)
map(`direction2`, choose_logo)
}
)
输出:
现在我希望也将它应用到 direction1
,但由于它有一个 NA
值,我用 fmt_missing
函数将其转换为 -
,它会引发错误:Error in if (x == 0) { : missing value where TRUE/FALSE needed
.
请注意,我已经创建了一个 else if (x == '-')
条件,但它不起作用。任何人都可以处理问题吗?非常感谢。
fmt_missing
仅用于格式化,不会更改将馈入 choose_logo 的 x
的值。所以它仍然是NA。你必须先抓住这种情况,因为例如5 == NA
既不是 TRUE
也不是 FALSE
:
library(tidyverse)
library(gt)
df <- structure(list(type = c("v1", "v2", "v3"), value = c(
1.2, 1.6,
-0.3
), direction1 = c(0.1, NA, 0), direction2 = c(0.0, -0.1, 1.2)), class = "data.frame", row.names = c(NA, -3L))
df
df %>%
gt() %>%
fmt_missing(
columns = c("direction1", "direction2"),
missing_text = "-"
) %>%
text_transform(
locations = cells_body(columns = c(`direction2`, `direction1`)),
fn = function(x) {
`direction1` <- as.numeric(x)
`direction2` <- as.numeric(x)
choose_logo <- function(x) {
if(is.na(x)) {
return(
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"))
)
}
if (x == 0) {
gt::html(glue::glue("<span style='color:#bfbfbf;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-right", fill = 'gray'))
} else if (x > 0) {
gt::html(glue::glue("<span style='color:#CF000F;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-up", fill = 'red'))
} else if (x < 0) {
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-down", fill = 'blue'))
}
}
map(`direction1`, choose_logo)
map(`direction2`, choose_logo)
}
)
对于数据集df
如下:
df <- structure(list(type = c("v1", "v2", "v3"), value = c(1.2, 1.6,
-0.3), direction1 = c(0.1, NA, 0), direction2 = c(0.0, -0.1, 1.2
)), class = "data.frame", row.names = c(NA, -3L))
使用下面的代码,我可以通过条件着色和添加 arrow
s:
direction2
df %>%
gt() %>%
fmt_missing(
columns = c('direction1', 'direction2'),
missing_text = "-"
) %>%
text_transform(
locations = cells_body(columns = c(`direction2`)),
fn = function(x){
`direction1` <- as.numeric(x)
`direction2` <- as.numeric(x)
choose_logo <-function(x){
if (x == 0){
gt::html(glue::glue("<span style='color:#bfbfbf;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-right", fill = gray))
} else if (x > 0){
gt::html(glue::glue("<span style='color:#CF000F;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-up", fill = red))
} else if (x < 0) {
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-down", fill = blue))
}
else if (x == '-') {
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("", fill = tjd_blue))
}
}
map(`direction1`, choose_logo)
map(`direction2`, choose_logo)
}
)
输出:
现在我希望也将它应用到 direction1
,但由于它有一个 NA
值,我用 fmt_missing
函数将其转换为 -
,它会引发错误:Error in if (x == 0) { : missing value where TRUE/FALSE needed
.
请注意,我已经创建了一个 else if (x == '-')
条件,但它不起作用。任何人都可以处理问题吗?非常感谢。
fmt_missing
仅用于格式化,不会更改将馈入 choose_logo 的 x
的值。所以它仍然是NA。你必须先抓住这种情况,因为例如5 == NA
既不是 TRUE
也不是 FALSE
:
library(tidyverse)
library(gt)
df <- structure(list(type = c("v1", "v2", "v3"), value = c(
1.2, 1.6,
-0.3
), direction1 = c(0.1, NA, 0), direction2 = c(0.0, -0.1, 1.2)), class = "data.frame", row.names = c(NA, -3L))
df
df %>%
gt() %>%
fmt_missing(
columns = c("direction1", "direction2"),
missing_text = "-"
) %>%
text_transform(
locations = cells_body(columns = c(`direction2`, `direction1`)),
fn = function(x) {
`direction1` <- as.numeric(x)
`direction2` <- as.numeric(x)
choose_logo <- function(x) {
if(is.na(x)) {
return(
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"))
)
}
if (x == 0) {
gt::html(glue::glue("<span style='color:#bfbfbf;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-right", fill = 'gray'))
} else if (x > 0) {
gt::html(glue::glue("<span style='color:#CF000F;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-up", fill = 'red'))
} else if (x < 0) {
gt::html(glue::glue("<span style='color:#1F3A93;font-face:bold;font-size:16px;'>{x}</span>"), fontawesome::fa("arrow-down", fill = 'blue'))
}
}
map(`direction1`, choose_logo)
map(`direction2`, choose_logo)
}
)