如何使 gt_sparkline() 与所有 NA 的案例第一行协调

How to make gt_sparkline() coordinate with case one row with all NAs

对于以下 df,如果 type 的 none 与所有 NANA_real_,则以下代码有效:

library(gt)
library(gtExtras)

df <- structure(
  list(
    type = c("v1", "v2", 'v3'),
    `2017-06` = c(300, 100, NA_real_),
    `2017-07` = c(10, 900, NA_real_), `2017-08` = c(500, NA, NA_real_), 
    `2017-09` = c(NA, 650, 5), `2017-10` = c(850, 600, NA_real_)
  ),
  class = "data.frame", row.names = c(NA, -3L)
)

df_list <- df %>%
  rowwise() %>%
  mutate(data = list(c_across(-type))) %>%
  select(type, data) %>% 
  ungroup() 

df_list %>% 
  # remove the NA values from the vectors.
  mutate(data = purrr::map(data, ~.x[!is.na(.x) & length(.x) >=2])) %>%
  gt() %>%
  gt_sparkline(data) 

输出:

如果我用所有 NA 更改 v3 并重新运行其余代码:

df <- structure(
  list(
    type = c("v1", "v2", 'v3'),
    `2017-06` = c(300, 100, NA),
    `2017-07` = c(10, 900, NA), `2017-08` = c(500, NA, NA), 
    `2017-09` = c(NA, 650, NA), `2017-10` = c(850, 600, NA)
  ),
  class = "data.frame", row.names = c(NA, -3L)
)

我会得到一个错误:

Error: Tibble columns must have compatible sizes.
* Size 2: Existing data.
* Size 0: Column `y`.
i Only values of size one are recycled.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: In max(vals, na.rm = TRUE) :
  no non-missing arguments to max; returning -Inf
2: In min(vals, na.rm = TRUE) :
  no non-missing arguments to min; returning Inf

如果一个变量每个月都包含 NA,如何通过不绘制迷你图来处理此异常?谢谢。

注意:我尝试通过设置 purrr::map(data, ~.x[!is.na(.x) & length(.x) >=2]) 使一个变量至少有 2 个非 NAs 值时绘制迷你图,但它不起作用,因为 v3画的是1点,但是有效值只有一个,就是5。我怎么弄对了?

对于只有缺失值或只有一个 non-missing 价值:

df_list %>% 
  # remove the NA values from the vectors.
  mutate(data = purrr::map(data, function(.x) {
    .x <- .x[!is.na(.x)]; if (length(.x) < 2) NA_real_ else .x
  })) %>%
  gt() %>%
  gt_sparkline(data)