Skip to content

rename_with with empty column selection fails w/ dplyr 1.1.0. #6688

@lschneiderbauer

Description

@lschneiderbauer

Hi,

due to the size check in the new dplyr version, dplyr 1.1.0, renames with empty column selections don't work anymore (since it expects a vector of functions of size 0).
This is of course not a big problem when used interactively, since one can simply omit the whole statement.
But when used programmatically in a data pipeline it forces the programmer to always treat this case separately (if for instance the .cols argument is defined by external dynamical parameters).

I would expect this function to simply do nothing if no columns are selected, and not throw an error.

library(dplyr, warn.conflicts = FALSE)

tibble(col = c(1,3)) %>%
  rename_with(
    .cols = contains("test"),
    .fn = ~ paste0("blah.", .)
  )
#> Error in `rename_with()`:
#> ! `.fn` must return a vector of length 0, not 1.

#> Backtrace:
#>     ▆
#>  1. ├─tibble(col = c(1, 3)) %>% ...
#>  2. ├─dplyr::rename_with(...)
#>  3. └─dplyr:::rename_with.data.frame(., .cols = contains("test"), .fn = ~paste0("blah.", .))
#>  4.   └─cli::cli_abort("{.arg .fn} must return a vector of length {length(sel)}, not {length(new)}.")
#>  5.     └─rlang::abort(...)

Created on 2023-02-06 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions