jthomasmock / gtExtras

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

gt_plt_dist: same_limit not working as expected #101

Closed DanielEWeeks closed 11 months ago

DanielEWeeks commented 12 months ago

Prework

Description

Part I: Setting same_limit to FALSE works as expected when type="histogram", but appears to have no effect when type="density"

Part II: If the set of lists being sent to gt_plt_dist contains an empty list, then this works as expected when same_limit is TRUE but dies in an error when same_limit is FALSE.

Reproducible example

Part I

df <- tibble(trait=c("age","time"),
             Distribution=c(list(runif(50, min=0, max=4),runif(50, min=5, max=10))))

# The limits change here when same_limit is toggled
df %>% gt() %>% gt_plt_dist(Distribution, type="histogram", same_limit = TRUE)
df %>% gt() %>% gt_plt_dist(Distribution, type="histogram", same_limit = FALSE)

# These two sets of plots appear identical even though same_limit has been toggled
df %>% gt() %>% gt_plt_dist(Distribution, type="density", same_limit = TRUE)
df %>% gt() %>% gt_plt_dist(Distribution, type="density", same_limit = FALSE)

Part II

df <- tibble(trait=c("age","weight","time"),
             Distribution=c(list(rnorm(50),NA, runif(50, min=5, max=10))))

# This runs without an error
df %>% gt() %>% gt_plt_dist(Distribution, same_limit = TRUE)

# This stops with an error
df %>% gt() %>% gt_plt_dist(Distribution, same_limit = FALSE)

The last line of code above generates this error:

Error in density.default(data_in, bw = bw) : 'x' contains missing values

Expected result

Part I: Setting same_limit to FALSE should shift the scales of the densities to have similar visual limits when type="density"

Part II: Setting same_limit to FALSE should not generate an error when some of the lists of values are NA.

The reason I have some rows with empty value lists is I am trying to insert spark plots into a table initially created by the table1 R command - it put the trait name in an empty row by itself, followed by summary statistics on the next line.

Session info

R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.7.6

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] knitr_1.42      gtExtras_0.5.0  gt_0.9.0        tidylog_1.0.2   lubridate_1.9.2
 [6] forcats_1.0.0   stringr_1.5.0   dplyr_1.1.3     purrr_1.0.1     readr_2.1.4    
[11] tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.1   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] clisymbols_1.2.0  tidyselect_1.2.0  xfun_0.37         rematch2_2.1.2   
 [5] paletteer_1.5.0   colorspace_2.1-0  vctrs_0.6.3       generics_0.1.3   
 [9] htmltools_0.5.4   yaml_2.3.7        utf8_1.2.3        rlang_1.1.1      
[13] pillar_1.9.0      glue_1.6.2        withr_2.5.0       binom_1.1-1.1    
[17] lifecycle_1.0.3   munsell_0.5.0     gtable_0.3.1      ragg_1.2.5       
[21] fontawesome_0.5.0 evaluate_0.20     labeling_0.4.2    tzdb_0.3.0       
[25] fastmap_1.1.1     fansi_1.0.4       scales_1.2.1      farver_2.1.1     
[29] systemfonts_1.0.4 textshaping_0.3.6 hms_1.1.2         digest_0.6.31    
[33] stringi_1.7.12    grid_4.2.2        cli_3.6.0         tools_4.2.2      
[37] magrittr_2.0.3    sass_0.4.5        pkgconfig_2.0.3   ellipsis_0.3.2   
[41] xml2_1.3.3        timechange_0.2.0  rmarkdown_2.24    svglite_2.1.1    
[45] httr_1.4.5        rstudioapi_0.14   R6_2.5.1          compiler_4.2.2   
jthomasmock commented 11 months ago

Howdy @DanielEWeeks - thanks for reporting. My kingdom for no NAs 😢

I have a local fix and will push to GH shortly. Using your reprex as-is, here's it working locally.

library(dplyr)
library(gt)
library(gtExtras)

packageVersion("gtExtras")
#> [1] '0.5.0.9000'

df <- tibble(trait=c("age","weight","time"),
             Distribution=c(list(rnorm(50),NA, runif(50, min=5, max=10))))

# This runs without an error
df %>% gt() %>% gt_plt_dist(Distribution, same_limit = TRUE) |> gt_reprex_image()

# This stops with an error
df %>% gt() %>% gt_plt_dist(Distribution, same_limit = FALSE) |> gt_reprex_image()

Created on 2023-10-03 by the reprex package (v2.0.1)

DanielEWeeks commented 11 months ago

Wonderful! Thank you for fixing so fast.

I'm really excited about adding spark plots to our descriptive tables!