Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for empty tidyselect quosure #1548

Closed
sda030 opened this issue Apr 15, 2024 · 2 comments · Fixed by #1570
Closed

Better error message for empty tidyselect quosure #1548

sda030 opened this issue Apr 15, 2024 · 2 comments · Fixed by #1570
Labels
bug an unexpected problem or unintended behavior strings 🎻 tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day

Comments

@sda030
Copy link

sda030 commented Apr 15, 2024

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
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
Copy link
Member

Thanks for the report!

Note to self:

This occurs because from_vars here is integer():

from_vars <- tidyselect::eval_select(expr(c(...)), data, allow_rename = FALSE)

Resulting in an NA for first_pos here:

first_pos <- which(names(data) %in% names(from_vars))[1]

I think we have two options:

  • Error if from_vars is empty
  • Return a column named using col with each cell filled with "", i.e. the "initial" reduction value that would theoretically be built upon if you would have actually selected columns. It would be positioned at the end of the data frame.

@catalamarti
Copy link
Contributor

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

catalamarti added a commit to catalamarti/tidyr that referenced this issue Aug 15, 2024
DavisVaughan pushed a commit to catalamarti/tidyr that referenced this issue Aug 27, 2024
DavisVaughan added a commit that referenced this issue Aug 27, 2024
* unite works for empty selections

fixes #1548

* NEWS bullet

* Tweak tests

* A little modernization

* Bah, whitespace

---------

Co-authored-by: Davis Vaughan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior strings 🎻 tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants