jthomasmock / gtExtras

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

Error when using glt_plt_bar #7

Closed benho89 closed 2 years ago

benho89 commented 2 years ago

Hi. I'm getting an error returned in my console when I try and use gt_plt_bar, even with the example in the reference documentation (below). The error reads:

Error in match.arg(units) : 'arg' should be one of “in”, “cm”, “mm”

Not sure what I'm missing, so I'm wondering if you know of anything that may be the cause of this? Thanks.

library(gt)

gt_plt_bar_tab <- mtcars %>%
  head() %>%
  gt() %>%
  gt_plt_bar(column = mpg, keep_column = TRUE)
jthomasmock commented 2 years ago

Howdy @benho89 !

That's likely an error from ggplot2::ggsave().

I don't get and haven't gotten that error, but that could occur when the bar is temporarily saved to disk via ggsave() and then brought back into the table.

gtExtras is still under very active development, and gt_plt_bar() was recently switched from pure HTML to a ggplot2 based output, so I would recommend updating gtExtras. I am very curious about the ggplot2 and gtExtras version just to confirm.

Can you confirm which package version you are using?

Please report a reprex like the one below:

library(gt)
library(gtExtras)

packageVersion("gtExtras")
#> [1] '0.2.3'
packageVersion("ggplot2")
#> [1] '3.3.5.9000'
mtcars %>% 
  head() %>% 
  gt() %>% 
  gt_plt_bar(mpg) %>% 
  gtsave("test-plot.png")

Created on 2021-09-24 by the reprex package (v2.0.1)

benho89 commented 2 years ago

Thanks for your reply, @jthomasmock! Here's my reprex. Am I understanding correctly that I need to update my ggplot2 package?

library(gt)
#> Warning: package 'gt' was built under R version 4.0.5
library(gtExtras)
library(reprex)
#> Warning: package 'reprex' was built under R version 4.0.5

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

packageVersion("ggplot2")
#> [1] '3.3.5'

mtcars %>%
  head() %>%
  gt() %>%
  gt_plt_bar(mpg) %>%
  gtsave("test-plot.png")

Created on 2021-09-25 by the reprex package (v2.0.1)

I have now tried gt_plt_bar() again with my other data and it now appears to be working. Strange! The only difference I made from last time when the error was being returned was moving the gt_plt_bar() argument right after gt() (like in your example above) rather than having it come after gt() %>% gt_img_rows(), which is what I had originally.

jthomasmock commented 2 years ago

Hi @benho89 - no need to update ggplot2, just wanted to see where your package environment was.

I'm glad you were able to come to a resolution, and I'll leave the issue open as I explore further.

jthomasmock commented 2 years ago

I think this is resolved now, with latest refactoring. Please let me know @benho89 if you run into further issues.

Ryo-N7 commented 2 years ago

Hi Tom,

I'm still getting the same issue on the latest version 0.2.9 38baa0bc.

I thought I messed something up with the data I was using but got the same errors using the example code in the docs:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tibble)
library(gt)
library(gtExtras)

## bullet chart
bullet_df <- tibble::rownames_to_column(mtcars) %>%
 dplyr::select(rowname, cyl:drat, mpg) %>%
 dplyr::group_by(cyl) %>%
 dplyr::mutate(target_col = mean(mpg)) %>%
 dplyr::slice_sample(n = 3) %>%
 dplyr::ungroup() 

bullet_df
#> # A tibble: 9 x 7
#>   rowname             cyl  disp    hp  drat   mpg target_col
#>   <chr>             <dbl> <dbl> <dbl> <dbl> <dbl>      <dbl>
#> 1 Volvo 142E            4  121    109  4.11  21.4       26.7
#> 2 Merc 230              4  141.    95  3.92  22.8       26.7
#> 3 Porsche 914-2         4  120.    91  4.43  26         26.7
#> 4 Mazda RX4             6  160    110  3.9   21         19.7
#> 5 Merc 280C             6  168.   123  3.92  17.8       19.7
#> 6 Mazda RX4 Wag         6  160    110  3.9   21         19.7
#> 7 Hornet Sportabout     8  360    175  3.15  18.7       15.1
#> 8 Chrysler Imperial     8  440    230  3.23  14.7       15.1
#> 9 Duster 360            8  360    245  3.21  14.3       15.1

bullet_df %>%
 gt() %>%
 gt_plt_bullet(column = mpg, target = target_col, width = 45,
               colors = c("lightblue", "black"))
#> Error in match.arg(units): 'arg' should be one of "in", "cm", "mm"

## inline bars
mtcars %>%
  dplyr::select(cyl:wt, mpg) %>% 
  head() %>%
  gt() %>%
  gt_plt_bar(column = mpg, keep_column = TRUE, width = 35)
#> Error in match.arg(units): 'arg' should be one of "in", "cm", "mm"

sessionInfo()
#> R version 4.0.5 (2021-03-31)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19043)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> system code page: 65001
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] gtExtras_0.2.9 gt_0.3.1       tibble_3.1.2   dplyr_1.0.6   
#> 
#> loaded via a namespace (and not attached):
#>  [1] pillar_1.6.3      compiler_4.0.5    highr_0.9         tools_4.0.5      
#>  [5] digest_0.6.27     checkmate_2.0.0   evaluate_0.14     lifecycle_1.0.1  
#>  [9] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.11      reprex_2.0.1     
#> [13] rstudioapi_0.13   cli_3.0.1         DBI_1.1.1         yaml_2.2.1       
#> [17] xfun_0.23         paletteer_1.4.0   withr_2.4.2       styler_1.5.1     
#> [21] stringr_1.4.0     knitr_1.33        generics_0.1.0    fs_1.5.0         
#> [25] vctrs_0.3.8       webshot_0.5.2     grid_4.0.5        tidyselect_1.1.1 
#> [29] fontawesome_0.2.2 glue_1.4.2        R6_2.5.1          fansi_0.4.2      
#> [33] rmarkdown_2.8     rematch2_2.1.2    purrr_0.3.4       ggplot2_3.3.3    
#> [37] magrittr_2.0.1    backports_1.2.1   scales_1.1.1      ellipsis_0.3.2   
#> [41] htmltools_0.5.1.1 assertthat_0.2.1  colorspace_2.0-1  utf8_1.2.1       
#> [45] stringi_1.7.4     munsell_0.5.0     crayon_1.4.1
Created on 2021-10-02 by the reprex package (v2.0.1)
jthomasmock commented 2 years ago

Hi @Ryo-N7 :

Can you try this code?

test_plot <- head(mtcars) %>% 
  ggplot(aes(x = disp, y = mpg)) +
  geom_point()

ggsave("test-plot.svg",
       plot = test_plot, dpi = 30, height = 70, width = 70,
       units = "px")

And do the other plotting functions work for you?

Ryo-N7 commented 2 years ago

Yep, I get the same error Error in match.arg(units): 'arg' should be one of "in", "cm", "mm" using that snippet of code.

I tried out a few more:

jthomasmock commented 2 years ago

I am still unable to reproduce this error on R 4.0 or 4.1 on Mac/Linux. 🤔

I am glad to hear that we seemed to have isolated it to ggsave().

I have refactored gt_plt_bullet() to use mm instead of px in latest (gtExtras v0.2.11) unsure if that will help you out as I'm not able to reprex. 😢

Can you try installing very latest version of gtExtras?

jthomasmock commented 2 years ago

Closing for now as I believe the error is resolved by not using px.