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` gives an error with "histogram", "boxplot" and "rug_strip" #125

Closed stefanoanzani closed 1 month ago

stefanoanzani commented 5 months ago

Description

I wanted to add the distribution of likert answers into a summary table for a survey like this.

image

I am able to add the histogram if I set the "same_limit" argument to "FALSE", but if I leave (or set) the value to TRUE I get the following error:

Error in as.character(call[[1]]) : 
  cannot coerce type 'closure' to vector of type 'character'

It also happens when I try "rug_strip" and "boxplot", it doesn't seem to happen when I use "density"

Reproducible example

Initial wrangling:

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

table_df <- iris |>
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") |>
  group_by(Vars) |>
  summarise(obs = n(),
            mean = round(mean(Values),2),
            median = round(median(Values),2),
            iris_data = list(Values) # adds the data as a list to the end of the df
  )

Working code:

table_df  |>
  gt() |>
  gt_plt_dist(iris_data, 
                        type = "histogram",
                        same_limit = FALSE
  )

Code giving the error:


table_df |>
  gt() |>
  gt_plt_dist(iris_data, 
                        type = "histogram",
                        same_limit = TRUE
  ) 

Session info

sessionInfo()
#> R version 4.3.0 (2023-04-21 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Italian_Italy.utf8  LC_CTYPE=Italian_Italy.utf8   
#> [3] LC_MONETARY=Italian_Italy.utf8 LC_NUMERIC=C                  
#> [5] LC_TIME=Italian_Italy.utf8    
#> 
#> time zone: Europe/Rome
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.31     fastmap_1.1.1     xfun_0.39         glue_1.6.2       
#>  [5] knitr_1.42        htmltools_0.5.5   rmarkdown_2.21    lifecycle_1.0.4  
#>  [9] cli_3.6.1         reprex_2.0.2      withr_2.5.2       compiler_4.3.0   
#> [13] rstudioapi_0.15.0 tools_4.3.0       evaluate_0.21     yaml_2.3.7       
#> [17] rlang_1.1.1       fs_1.6.2

Created on 2024-04-19 with reprex v2.0.2

jthomasmock commented 1 month ago

Hmm, I'm not able to reproduce this -- can you try the latest CRAN release? I don't see gtExtras loaded in your session info.

stefanoanzani commented 1 month ago

Ok I've tested again in a new posit cloud workspace and indeed I wasn't able to reproduce this as well, whereas it's still bugging me locally, so my best guess is that I have some issues with the packages installed locally. sorry about that, i tried ruling out local issues before posting but i guess it's better if i test my reprex in the cloud the next time to make sure!