Search code examples
rgt

Apply gt_sparkline() to multiple numeric columns with NAs generates error


Given a sample data as follow:

df <- structure(list(variable = c("cabbage", "radish", "apple", "pear", 
"monitor", "mouse", "camera", "calculator"), X2021.q1 = c(6, 
5, 6, 2.9, 2000, NA, 600, 35), X2021.q2 = c(4L, 8L, 4L, NA, 2001L, 
11L, 601L, NA), X2021.q3 = c(8, NA, 5, 4.9, 2002, 12, 602, 37
), X2021.q4 = c(8L, 3L, 2L, 7L, NA, 10L, 580L, 33L)), class = "data.frame", row.names = c(NA, 
-8L))

Out:

enter image description here I'm trying to plot time series for each variable using:

df %>% 
  mutate(data=list(c_across(where(is.numeric)))) %>% 
  gt() %>% 
  gt_sparkline(data)

But it raises an error: Error in if (med_y_rnd > 0) { : missing value where TRUE/FALSE needed.

How could I fix this issue? Thanks.

Reference link:

https://jthomasmock.github.io/gtExtras/


Solution

  • I think problems are caused by NAs.

    Try na.omit() in pipe,

    df %>% 
      na.omit() %>%
      mutate(data=list(c_across(where(is.numeric)))) %>%
      gt() %>% 
      gt_sparkline(data)
    

    enter image description here