From 094190c5ed17841cffaf5e6f194221b615fc7711 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 15:58:57 +0100 Subject: [PATCH] no lists --- R/data_rename.R | 34 +++++++++++++++---------------- man/data_partition.Rd | 2 +- man/data_rename.Rd | 6 +----- man/rescale.Rd | 4 ---- tests/testthat/test-data_rename.R | 7 ++++--- 5 files changed, 22 insertions(+), 31 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 28fd61704..1c81c9ed6 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -9,7 +9,7 @@ #' possible pipe-workflow. #' #' @inheritParams extract_column_names -#' @param data A data frame, or an object that can be coerced to a data frame. +#' @param data A data frame. #' @param replacement Character vector. Can be one of the following: #' - A character vector that indicates the new names of the columns selected #' in `select`. `select` and `replacement` must be of the same length. @@ -48,10 +48,6 @@ #' `select` can also be a named character vector. In this case, the names are #' used to rename the columns in the output data frame. See 'Examples'. #' -#' `data_rename()` also works with list-inputs, but in this case, `select` must -#' be a character vector with valid names. It is not possible to use -#' select-helpers when `data` is a list. -#' #' @return A modified data frame. #' #' @examples @@ -94,6 +90,10 @@ data_rename <- function(data, verbose = TRUE, pattern = NULL, ...) { + # check for valid input + if (!is.data.frame(data)) { + insight::format_error("Argument `data` must be a data frame.") + } # If the user does data_rename(iris, pattern = "Sepal.Length", "length"), # then "length" is matched to select by position while it's the replacement # => do the switch manually @@ -108,19 +108,17 @@ data_rename <- function(data, insight::format_warning("In `data_rename()`, argument `safe` is no longer used and will be removed in a future release.") # nolint } - # change all names if no pattern specified - this does not work for lists! - if (!is.list(data) || is.data.frame(data)) { - select <- .select_nse( - select, - data, - exclude = NULL, - ignore_case = NULL, - regex = NULL, - allow_rename = TRUE, - verbose = verbose, - ifnotfound = "error" - ) - } + # change all names if no pattern specified + select <- .select_nse( + select, + data, + exclude = NULL, + ignore_case = NULL, + regex = NULL, + allow_rename = TRUE, + verbose = verbose, + ifnotfound = "error" + ) # Forbid partially named "select", # Ex: if select = c("foo" = "Species", "Sepal.Length") then the 2nd name and diff --git a/man/data_partition.Rd b/man/data_partition.Rd index 23015e1b3..a76260caa 100644 --- a/man/data_partition.Rd +++ b/man/data_partition.Rd @@ -15,7 +15,7 @@ data_partition( ) } \arguments{ -\item{data}{A data frame, or an object that can be coerced to a data frame.} +\item{data}{A data frame.} \item{proportion}{Scalar (between 0 and 1) or numeric vector, indicating the proportion(s) of the training set(s). The sum of \code{proportion} must not be diff --git a/man/data_rename.Rd b/man/data_rename.Rd index a2cd83703..83a983dcc 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -18,7 +18,7 @@ data_rename( data_rename_rows(data, rows = NULL) } \arguments{ -\item{data}{A data frame, or an object that can be coerced to a data frame.} +\item{data}{A data frame.} \item{select}{Variables that will be included when performing the required tasks. Can be either @@ -114,10 +114,6 @@ possible pipe-workflow. \details{ \code{select} can also be a named character vector. In this case, the names are used to rename the columns in the output data frame. See 'Examples'. - -\code{data_rename()} also works with list-inputs, but in this case, \code{select} must -be a character vector with valid names. It is not possible to use -select-helpers when \code{data} is a list. } \examples{ # Rename columns diff --git a/man/rescale.Rd b/man/rescale.Rd index 0744ca3ba..f163a6e8c 100644 --- a/man/rescale.Rd +++ b/man/rescale.Rd @@ -135,10 +135,6 @@ Rescale variables to a new range. Can also be used to reverse-score variables \details{ \code{select} can also be a named character vector. In this case, the names are used to rename the columns in the output data frame. See 'Examples'. - -\code{data_rename()} also works with list-inputs, but in this case, \code{select} must -be a character vector with valid names. It is not possible to use -select-helpers when \code{data} is a list. } \section{Selection of variables - the \code{select} argument}{ diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index bcaa4e161..31dda49b3 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -227,7 +227,8 @@ test_that("works with lists", { setosa.a = structure(data.frame(1)), versicolor.a = structure(data.frame(2)) ) - new_names <- c("setosa, a", "versicolor, a") - out <- data_rename(result, select = names(result), replacement = new_names) - expect_named(out, new_names) + expect_error( + data_rename(result, select = names(result), replacement = c("a", "b")), + regex = "must be a data frame" + ) })