jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
193 stars 26 forks source link

Feature Request: Allow for missing row in `gt_sparkline()` #13

Closed ddsjoberg closed 2 years ago

ddsjoberg commented 2 years ago

Hello! gtExtras is so cool and cute!

How do you feel about allowing a missing row in gt_sparline()? For example, in the example below, the second row is all missing, and I would like to see the image in the first and third rows and a blank second row. (I get an error if list(NA) is replaced with NULL.)

What do you think?

library(gt)

mtcars %>%
  dplyr::group_by(cyl) %>%
  # must end up with list of data for each row in the input dataframe
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  dplyr::mutate(
    mpg_data = list(mpg_data[[1]], list(NA), mpg_data[[3]])
  ) %>%
  gt::gt() %>%
  gtExtras::gt_sparkline(mpg_data)
#> Warning in strsplit(x, split = ", ") %>% unlist() %>% as.double(): NAs
#> introduced by coercion
#> Warning in max(vals, na.rm = TRUE): no non-missing arguments to max; returning
#> -Inf
#> Warning in min(vals, na.rm = TRUE): no non-missing arguments to min; returning
#> Inf
#> Error in if (med_y_rnd > 0) {: missing value where TRUE/FALSE needed

Created on 2021-10-10 by the reprex package (v2.0.0)

jthomasmock commented 2 years ago

Thanks so much!

And oof, thanks for asking about this - I am still adding my tests and I'll make sure to include this scenario 🙃

I've added a lightweight "blank return" if a NA or NULL is for that row.

Using gtExtras v 0.2.17, with your example (slightly modified to include a NA and a NULL).

It now returns the below without a warning/error:

library(tidyverse)
library(gt)
library(gtExtras)

input_data <- mtcars %>%
  dplyr::group_by(cyl) %>%
  # must end up with list of data for each row in the input dataframe
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  dplyr::mutate(
    mpg_data = list(mpg_data[[1]], list(NA), list(NULL))
  )

input_data %>% 
  gt() %>% 
  gt_sparkline(mpg_data) %>% 
  gtsave("ex.png")

Created on 2021-10-11 by the reprex package (v2.0.1)

ddsjoberg commented 2 years ago

Ahhhh, gorgeous! Thank you @jthomasmock !!!

ddsjoberg commented 2 years ago

Hello Again @jthomasmock !

Thank you for this update! It's been great! I noticed that this code works flawlessly for sparkline and histogram, but errors with type = "density"

library(tidyverse)
library(gt)
library(gtExtras)

input_data <- mtcars %>%
  dplyr::group_by(cyl) %>%
  # must end up with list of data for each row in the input dataframe
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  dplyr::mutate(
    mpg_data = list(mpg_data[[1]], list(NA), list(NULL))
  )

input_data %>% 
  gt::gt() %>% 
  gtExtras::gt_sparkline(mpg_data, type = "density")
#> Error in quantile.default(as.numeric(x), c(0.25, 0.75), na.rm = na.rm, : missing values and NaN's not allowed if 'na.rm' is FALSE

Created on 2021-10-18 by the reprex package (v2.0.1)

jthomasmock commented 2 years ago

Thanks for heads up! I've added another internal early return()/missing data handling and added gt_sparkline + missing data handling to tests. Working as seen below in latest package version.

library(gt)
library(gtExtras)

input_data <- mtcars %>%
  dplyr::group_by(cyl) %>%
  # must end up with list of data for each row in the input dataframe
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  dplyr::mutate(
    mpg_data = list(mpg_data[[1]], list(NA), list(NULL))
  )

# Spark -------------------------------------------------------------------

input_data %>%
  gt::gt() %>%
  gt_sparkline(.data$mpg_data, type = "sparkline") %>%
  gt::gtsave("temp-spark.png")

# Histogram ---------------------------------------------------------------

input_data %>%
  gt::gt() %>%
  gt_sparkline(.data$mpg_data, type = "histogram") %>%
  gt::gtsave("temp-hist.png")

# Density -----------------------------------------------------------------

input_data %>%
  gt::gt() %>%
  gt_sparkline(.data$mpg_data, type = "density") %>%
  gt::gtsave("temp-dens.png")

Created on 2021-10-18 by the reprex package (v2.0.1)

ddsjoberg commented 2 years ago

you are amazingly quick! thank you! looks great!

ddsjoberg commented 2 years ago

The gtsummary + gtExtras + sparkline table looks super cute 😍

trial %>%
  select(age, marker) %>%
  tbl_summary(missing = "no") %>%
  add_sparkline()

image http://www.danieldsjoberg.com/bstfun/reference/add_sparkline.html

jthomasmock commented 2 years ago

Glad it's working in gtsummary now!