Skip to content

rename_with issue #6871

@linuxiaobai

Description

@linuxiaobai

Looks like from the version v1.1.0, the function rename_with will get error if no variables are selected. In my case, I have data from many spreedsheets, and I need to rename variable ABC to AAA if variable ABC is available. If ABC doesn't exist in one spreadsheet, then nothing to do. Actually I have several variables like ABC. If I have to judge if all those variables exist in all the spreedsheets, the logic will be quality complex. So I have tired below. It seems rename_with has a bug for the last piece of code.

# library
library(dplyr)

test=iris
names(test)

# this code works for the verion lower than v1.1.0
test0<-test %>% rename_with("newname",starts_with("Spec2ie"))

# this code works and no variable name changes
test1<-test %>% rename_with(~character(0),starts_with("Spec2ie"))
names(test1)

# this code don't work
test2<-test %>% rename_with(~ifelse(length(.)==0,character(0),"newname"),
                        starts_with("Spec2ie"))
# Error in `rename_with()`:
# ! `.fn` must return a vector of length 0, not 1.
# Run `rlang::last_trace()` to see where the error occurred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions