[英]R gt table make NA values not appear
我在gt
表中有 20 個數字列,我根據它們的值着色。 此外,我讓每一行在灰色和白色之間交替。 我想讓表格不顯示 NA 值。
這是一個示例表:
library(gt)
library(magrittr)
id <- 1:10
res1 <- sample(1:20, 10)
nm2 <-
c("red",
"purple",
"green",
"turtle",
"name",
"dog",
"cat",
"horse",
"space",
"planet")
res3 <- sample(1:20, 10)
nm4 <- nm2
# Add NAs
res1[6] <- NA
nm2[6] <- "NA"
res3[4] <- NA
nm4[4] <- "NA"
df <- data.frame(id, res1, nm2, res3, nm4)
pal <-
RColorBrewer::brewer.pal(8, "RdYlGn") %>% gt::adjust_luminance(-1.0)
plot <-df %>%
gt() %>%
tab_style(style = cell_fill("lightgrey"),
locations = cells_body(rows = seq(1, nrow(df), by = 2))) %>%
data_color(
columns = colnames(df)[grep("res", colnames(df))],
colors = scales::col_numeric(palette = pal,
domain = c(0, 20))
)
我目前知道如何做到這一點的唯一方法是根據是否存在 NA 手動更改每列的文本和顏色,但是這樣做:
tab_style(
style = list(cell_text("white"),
cell_fill("white")),
locations = cells_body(
columns = c("res1", "nm2"),
rows = is.na(df$res1) & row_number(df$res1) ==seq(0, nrow(df), by = 2)
)
) %>%
tab_style(
style = list(cell_text("lightgrey"),
cell_fill("lightgrey")),
locations = cells_body(
columns = c("res1", "nm2"),
rows = is.na(df$res1) & row_number(df$res1) ==seq(1, nrow(df), by = 2)
)
)
無論該行中的任何值是否為 NA,我都希望顯示所有 10 個 ID。 反正有沒有更有效地做到這一點?
TBMK 您手動執行此操作的方法是 go。 但是,恕我直言,您可以通過將na.color
使用的data_color
設置為"white"
來稍微簡化代碼,這樣您只需處理灰色背景行中的NA
值。 此外,您可以通過使用自定義 function 來進一步簡化,而不是為每列復制代碼。
注意 1:我使用seq_along(...) %% 2 == 1
將tab_style
僅應用於不均勻的行。
注意 2:我稍微調整了您的示例數據以在不均勻的行中包含 NA,即第 5 行。
library(gt)
library(magrittr)
tab_style_na <- function(data, col) {
tab_style(
data,
style = list(cell_fill("lightgrey")),
locations = cells_body(
columns = all_of(col),
rows = is.na(!!sym(col)) & seq_along(!!sym(col)) %% 2 == 1
)
)
}
df %>%
gt() %>%
tab_style(
style = cell_fill("lightgrey"),
locations = cells_body(rows = seq(1, nrow(df), by = 2))
) %>%
data_color(
columns = colnames(df)[grep("res", colnames(df))],
colors = scales::col_numeric(
palette = pal,
domain = c(0, 20),
na.color = "transparent"
)
) %>%
tab_style_na("res1") %>%
tab_style_na("res3")
數據
set.seed(123)
id <- 1:10
res1 <- sample(1:20, 10)
nm2 <-
c(
"red",
"purple",
"green",
"turtle",
"name",
"dog",
"cat",
"horse",
"space",
"planet"
)
res3 <- sample(1:20, 10)
nm4 <- nm2
# Add NAs
res1[6] <- NA
nm2[6] <- "NA"
res3[5] <- NA
nm4[5] <- "NA"
df <- data.frame(id, res1, nm2, res3, nm4)
library(dplyr)
library(gt)
library(magrittr)
id <- 1:10
res1 <- sample(1:20, 10)
nm2 <-
c("red",
"purple",
"green",
"turtle",
"name",
"dog",
"cat",
"horse",
"space",
"planet")
res3 <- sample(1:20, 10)
nm4 <- nm2
# Add NAs
res1[6] <- NA
nm2[6] <- "NA"
res3[4] <- NA
nm4[4] <- "NA"
df <- data.frame(id, res1, nm2, res3, nm4)
pal <-
RColorBrewer::brewer.pal(8, "RdYlGn") %>% gt::adjust_luminance(-1.0)
## omit NAs here
#df <- na.omit(df)
plot <-df %>%
gt() %>%
tab_style(style = cell_fill("lightgrey"),
locations = cells_body(rows = seq(1, nrow(df), by = 2))) %>%
data_color(
columns = colnames(df)[grep("res", colnames(df))],
colors = scales::col_numeric(palette = pal,
domain = c(0, 20),
na.color = "transparent")
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.