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

dplyr::rename_with() fails with dplyr 1.0.0 #160

Closed
jasopolis opened this issue Jul 9, 2020 · 1 comment
Closed

dplyr::rename_with() fails with dplyr 1.0.0 #160

jasopolis opened this issue Jul 9, 2020 · 1 comment
Labels
dplyr-compat 💞 dplyr compatibility issues feature a feature request or enhancement

Comments

@jasopolis
Copy link

I'm getting an error using rename_with() from dplyr 1.0.0 with a lazy_dt() -- it doesn't seem to be implemented. I haven't checked many other dplyr 1.0.0 additions, so I'm not sure if this is a known issue (or WIP) or a one-off function issue. Thanks!

mtcars %>% lazy_dt() %>% rename_with(str_to_upper) %>% as.data.table() %>% head()
# Error in UseMethod("rename_with") : 
#   no applicable method for 'rename_with' applied to an object of class "c('dtplyr_step_first', 'dtplyr_step')"
mtcars %>% lazy_dt() %>% rename_all(str_to_upper) %>% as.data.table() %>% head()
#    MPG CYL DISP  HP DRAT    WT  QSEC VS AM GEAR CARB
# 1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# 2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# 3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# 4: 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# 5: 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# 6: 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
@jasopolis jasopolis changed the title No support for dplyr::rename_with() from dplyr 1.0.0 dplyr::rename_with() fails with dplyr 1.0.0 Jul 9, 2020
@hadley hadley added dplyr-compat 💞 dplyr compatibility issues feature a feature request or enhancement labels Jan 25, 2021
@hadley
Copy link
Member

hadley commented Jan 27, 2021

Rough implementation

#' @importFrom dplyr rename_with
#' @export
rename_with.dtplyr_step <- function(.data, .fn, .cols = everything(), ...) {
  .fn <- as_function(.fn)
  sim_data <- simulate_vars(.data)
  cols <- tidyselect::eval_select(enquo(.cols), sim_data)

  to_change <- names(.data)[cols]
  names(to_change) <- .fn(to_change, ...)

  rename(.data, !!!to_change)
}

@hadley hadley closed this as completed in 49ec5f9 Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dplyr-compat 💞 dplyr compatibility issues feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants