tidyverse / tidyr

Tidy Messy Data
https://tidyr.tidyverse.org/
Other
1.37k stars 416 forks source link

Better error message for empty tidyselect quosure #1548

Closed sda030 closed 1 day ago

sda030 commented 4 months ago

The following empty tidyselect::all_of() would have given an empty data frame in dplyr::select() but here gives a rather uninformative error. Perhaps a "Selection is empty.".


Brief description of the problem

expect_variables <- c()
tidyr::unite(mtcars, col = "merged", tidyselect::all_of(expect_variables))
#> Error in `df_append()`:
#> ! `after` must be a whole number, not an integer `NA`.
#> ℹ This is an internal error that was detected in the tidyr package.
#>   Please report it at <https://github.com/tidyverse/tidyr/issues> with a reprex
#>   (<https://tidyverse.org/help/>) and the full backtrace.
---
Backtrace:
    ▆
 1. ├─mtcars %>% ...
 2. ├─tidyr::unite(...)
 3. └─tidyr:::unite.data.frame(...)
 4.   └─tidyr:::df_append(after = after)
 5.     └─tidyr:::check_number_whole(after, min = 0L, max = n, .internal = TRUE)
 6.       └─tidyr:::.rlang_types_check_number(...)
 7.         └─tidyr (local) .stop(x, what, ...)
 8.           └─tidyr:::stop_input_type(...)
 9.             └─rlang::abort(message, ..., call = call, arg = arg)

Created on 2024-04-15 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31 ucrt) #> os Windows 11 x64 (build 22621) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate nb.utf8 #> ctype nb.utf8 #> tz Europe/Oslo #> date 2024-04-15 #> pandoc 3.1.12.3 @ C:/PROGRA~1/Pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] RSPM (R 4.3.0) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.3) #> dplyr 1.1.4 2023-11-17 [1] RSPM (R 4.3.0) #> evaluate 0.23 2023-11-01 [1] RSPM (R 4.3.0) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.2) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.3) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.3) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.2) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.3) #> knitr 1.45 2023-10-30 [1] RSPM (R 4.3.0) #> lifecycle 1.0.4 2023-11-07 [1] RSPM (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.3) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.2.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.3) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.3) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.2) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2) #> R.utils 2.12.3 2023-11-18 [1] RSPM (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.3) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.3.1) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.2) #> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.3.3) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.3) #> styler 1.10.2 2023-08-29 [1] RSPM (R 4.3.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.2.3) #> tidyr 1.3.1 2024-01-24 [1] CRAN (R 4.3.2) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.3.3) #> utf8 1.2.4 2023-10-22 [1] RSPM (R 4.3.0) #> vctrs 0.6.5 2023-12-01 [1] RSPM (R 4.3.0) #> withr 3.0.0 2024-01-16 [1] RSPM (R 4.3.1) #> xfun 0.42 2024-02-08 [1] CRAN (R 4.3.2) #> yaml 2.3.8 2023-12-11 [1] RSPM (R 4.3.0) #> #> [1] C:/Users/py128/AppData/Local/R/win-library #> [2] C:/Program Files/R/R-4.3.2/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
DavisVaughan commented 4 months ago

Thanks for the report!

Note to self:

This occurs because from_vars here is integer(): https://github.com/tidyverse/tidyr/blob/c6c126a61f67a10b5ab9ce6bb1d9dbbb7a380bbd/R/unite.R#L48

Resulting in an NA for first_pos here: https://github.com/tidyverse/tidyr/blob/c6c126a61f67a10b5ab9ce6bb1d9dbbb7a380bbd/R/unite.R#L72

I think we have two options:

catalamarti commented 1 week ago

hi @DavisVaughan I will work in the second approach if that's fine