markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
449 stars 33 forks source link

`count()` gives error for code that works in `dplyr::count()` #740

Closed marianschmidt closed 1 year ago

marianschmidt commented 1 year ago

I am running into a few further issues with the dotless functions when running older scripts like this one:

#reprex tidytable count

library(tidyverse, warn.conflicts = FALSE)
library(tidytable, warn.conflicts = FALSE)
#> As of tidytable v0.9.0 dotless versions of functions are exported.
#> You can now use `arrange()`/`mutate()`/etc. directly.
#> Warning: tidytable was loaded after dplyr.
#> This can lead to most dplyr functions being overwritten by tidytable functions.
#> Warning: tidytable was loaded after tidyr.
#> This can lead to most tidyr functions being overwritten by tidytable functions.

iris %>%
  tidytable::count(Sepal.Width > Petal.Length)
#> Error in `tidyselect_locs()`:
#> ! Problem while evaluating `Sepal.Width > Petal.Length`.
#> Caused by error:
#> ! Objekt 'Sepal.Width' nicht gefunden

#> Backtrace:
#>      ▆
#>   1. ├─iris %>% tidytable::count(Sepal.Width > Petal.Length)
#>   2. ├─tidytable::count(., Sepal.Width > Petal.Length)
#>   3. │ ├─tidytable::count.(...)
#>   4. │ └─tidytable:::count..data.frame(...)
#>   5. │   └─tidytable::count(...)
#>   6. │     ├─tidytable::count.(...)
#>   7. │     └─tidytable:::count..tidytable(...)
#>   8. │       └─tidytable:::count_tally(...)
#>   9. │         └─tidytable::summarize(...)
#>  10. │           ├─tidytable::summarize.(...)
#>  11. │           └─tidytable:::summarize..tidytable(...)
#>  12. │             └─tidytable:::tidyselect_names(.df, !!.by)
#>  13. │               └─tidytable:::tidyselect_locs(.df, ...)
#>  14. │                 └─tidyselect::eval_select(expr(c(...)), .df)
#>  15. │                   └─tidyselect:::eval_select_impl(...)
#>  16. │                     ├─tidyselect:::with_subscript_errors(...)
#>  17. │                     │ └─rlang::try_fetch(...)
#>  18. │                     │   └─base::withCallingHandlers(...)
#>  19. │                     └─tidyselect:::vars_select_eval(...)
#>  20. │                       └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#>  21. │                         └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  22. │                           └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  23. │                             └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  24. │                               └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  25. │                                 └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  26. │                                   └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  27. │                                     └─tidyselect:::eval_context(expr, context_mask, call = error_call)
#>  28. │                                       ├─tidyselect:::with_chained_errors(...)
#>  29. │                                       │ └─rlang::try_fetch(...)
#>  30. │                                       │   ├─base::tryCatch(...)
#>  31. │                                       │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  32. │                                       │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  33. │                                       │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  34. │                                       │   └─base::withCallingHandlers(...)
#>  35. │                                       └─rlang::eval_tidy(as_quosure(expr, env), context_mask)
#>  36. └─base::.handleSimpleError(...)
#>  37.   └─rlang (local) h(simpleError(msg, call))
#>  38.     └─handlers[[1L]](cnd)
#>  39.       └─rlang::abort(msg, call = call, parent = cnd)

iris %>%
  dplyr::count(Sepal.Width > Petal.Length)
#>   Sepal.Width > Petal.Length   n
#> 1                      FALSE 100
#> 2                       TRUE  50

Created on 2023-03-01 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31 ucrt) #> os Windows 10 x64 (build 19044) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate German_Germany.utf8 #> ctype German_Germany.utf8 #> tz Europe/Berlin #> date 2023-03-01 #> pandoc 2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.2) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.2.2) #> data.table 1.14.8 2023-02-17 [1] CRAN (R 4.2.2) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.2) #> dplyr * 1.1.0 2023-01-29 [1] CRAN (R 4.2.2) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.1) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.2) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.2) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.2) #> forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.2.2) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.2) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.1) #> ggplot2 * 3.4.1 2023-02-10 [1] CRAN (R 4.2.2) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.1) #> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.1) #> hms 1.1.2 2022-08-19 [1] CRAN (R 4.2.1) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.2) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.2) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1) #> lubridate * 1.9.2 2023-02-10 [1] CRAN (R 4.2.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.1) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.1) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.1) #> purrr * 1.0.1 2023-01-10 [1] CRAN (R 4.2.2) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.1) #> readr * 2.1.4 2023-02-10 [1] CRAN (R 4.2.2) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.1) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.2) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.1) #> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.2.2) #> stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.2.2) #> styler 1.9.0 2023-01-15 [1] CRAN (R 4.2.2) #> tibble * 3.1.8 2022-07-22 [1] CRAN (R 4.2.1) #> tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.2.2) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.1) #> tidytable * 0.9.2 2023-01-13 [1] CRAN (R 4.2.2) #> tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.2.2) #> timechange 0.2.0 2023-01-11 [1] CRAN (R 4.2.2) #> tzdb 0.3.0 2022-03-28 [1] CRAN (R 4.2.1) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.2) #> vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.2.2) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.1) #> xfun 0.37 2023-01-31 [1] CRAN (R 4.2.2) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.2) #> #> [1] C:/Users/ga27jar/AppData/Local/R/win-library/4.2 #> [2] C:/Program Files/R/R-4.2.2/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
markfairbanks commented 1 year ago

This is the same issue as #739 - dplyr::count() uses mutate/transmute semantics, whereas tidytable uses selection semantics. At this point I can't change tidytable to match dplyr since tidytable has been using selection for so long.

You need to do this in tidytable.

library(tidytable, warn.conflicts = FALSE)

iris %>%
  mutate(check = Sepal.Width > Petal.Length) %>%
  count(check)
#> # A tidytable: 2 × 2
#>   check     n
#>   <lgl> <int>
#> 1 FALSE   100
#> 2 TRUE     50