R/rename_with.R

Defines functions rename_with_loud

Documented in rename_with_loud

#' Rename with, but Loudly
#'
#' @inheritParams dplyr::rename_with
#'
#' @return .data renamed
#' @importFrom dplyr everything
#' @concept compare
#' @export
#' @examples
#' tb <- tibble::tibble(pop = 10, pop_2020_est = 9, pop_white_2020 = 8, pop_black_2020 = 2)
#' rename_with_loud(tb, \(x) rem_suff(x, '_2020'))
rename_with_loud <- function(.data, .fn, .cols = everything(), ...) {
  old <- names(.data)

  .data <- dplyr::rename_with(.data, .fn, !!.cols, ...)

  new <- names(.data)

  x <- which(old != new)

  ul <- cli::cli_ul()
  cli::cli_text('Changed {length(x)} name{?s}.')
  lapply(seq_along(x), function(i) {
    cli::cli_li(paste0(old[x[i]], ' -> ', new[x[i]]))
  })
  cli::cli_end(ul)

  .data
}

Try the name package in your browser

Any scripts or data that you put into this service are public.

name documentation built on Sept. 9, 2025, 5:35 p.m.