From a4a83087b8beb9179abc31025f548aebee08ad62 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 16:51:43 +0100 Subject: [PATCH] `data_rename()`: "Error: Following variable(s) were not found" in version '0.13.0.17' (#572) * `data_rename()`: "Error: Following variable(s) were not found" in version '0.13.0.17' Fixes #571 * version * comment * styler, lintr * docs * no lists * Update tests/testthat/test-data_rename.R Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> * fix docs * fix * shorter if-condition * fix --------- Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> --- R/adjust.R | 4 ++-- R/data_rename.R | 7 ++++++- R/data_rescale.R | 18 ++++++++---------- R/data_rotate.R | 3 ++- R/data_to_long.R | 9 ++++++--- R/select_nse.R | 11 +++++------ man/adjust.Rd | 4 ++-- man/data_partition.Rd | 2 +- man/data_rename.Rd | 2 +- man/data_to_long.Rd | 2 +- man/rescale.Rd | 5 ----- tests/testthat/test-data_rename.R | 8 ++++++++ 12 files changed, 42 insertions(+), 33 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index 9cd5805e9..f4cf44f0a 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -30,7 +30,7 @@ #' #' @return A data frame comparable to `data`, with adjusted variables. #' -#' @examplesIf require("bayestestR", quietly = TRUE) && require("rstanarm", quietly = TRUE) && require("gamm4", quietly = TRUE) +#' @examplesIf all(insight::check_if_installed(c("bayestestR", "rstanarm", "gamm4"), quietly = TRUE)) #' adjusted_all <- adjust(attitude) #' head(adjusted_all) #' adjusted_one <- adjust(attitude, effect = "complaints", select = "rating") @@ -43,7 +43,7 @@ #' } #' #' # Generate data -#' data <- simulate_correlation(n = 100, r = 0.7) +#' data <- bayestestR::simulate_correlation(n = 100, r = 0.7) #' data$V2 <- (5 * data$V2) + 20 # Add intercept #' #' # Adjust diff --git a/R/data_rename.R b/R/data_rename.R index eb2415d67..1c81c9ed6 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -1,4 +1,5 @@ #' @title Rename columns and variable names +#' @name data_rename #' #' @description Safe and intuitive functions to rename variables or rows in #' data frames. `data_rename()` will rename column names, i.e. it facilitates @@ -8,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. @@ -89,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 diff --git a/R/data_rescale.R b/R/data_rescale.R index 5707e38f0..acfc08959 100644 --- a/R/data_rescale.R +++ b/R/data_rescale.R @@ -1,12 +1,13 @@ -#' Rescale Variables to a New Range +#' @title Rescale Variables to a New Range +#' @name rescale #' +#' @description #' Rescale variables to a new range. Can also be used to reverse-score variables #' (change the keying/scoring direction), or to expand a range. #' #' @inheritParams categorize #' @inheritParams extract_column_names #' @inheritParams standardize.data.frame -#' #' @param to Numeric vector of length 2 giving the new range that the variable #' will have after rescaling. To reverse-score a variable, the range should #' be given with the maximum value first. See examples. @@ -27,6 +28,11 @@ #' #' @inheritSection center Selection of variables - the `select` argument #' +#' @seealso See [makepredictcall.dw_transformer()] for use in model formulas. +#' @family transform utilities +#' +#' @return A rescaled object. +#' #' @examples #' rescale(c(0, 1, 5, -5, -2)) #' rescale(c(0, 1, 5, -5, -2), to = c(-5, 5)) @@ -62,14 +68,6 @@ #' # Specify list of multipliers #' d <- data.frame(x = 5:15, y = 5:15) #' rescale(d, multiply = list(x = 1.1, y = 0.5)) -#' -#' @inherit data_rename -#' -#' @return A rescaled object. -#' -#' @seealso See [makepredictcall.dw_transformer()] for use in model formulas. -#' @family transform utilities -#' #' @export rescale <- function(x, ...) { UseMethod("rescale") diff --git a/R/data_rotate.R b/R/data_rotate.R index 2bb39a85d..1a0cc3431 100644 --- a/R/data_rotate.R +++ b/R/data_rotate.R @@ -17,6 +17,8 @@ #' names. #' @param verbose Toggle warnings. #' +#' @inherit data_rename seealso +#' #' @return A (rotated) data frame. #' #' @examples @@ -36,7 +38,6 @@ #' data_rotate(x, colnames = TRUE) #' data_rotate(x, colnames = "c") #' -#' @inherit data_rename seealso #' @export data_rotate <- function(data, rownames = NULL, colnames = FALSE, verbose = TRUE) { # copy attributes diff --git a/R/data_to_long.R b/R/data_to_long.R index deffcc0cb..c44a8d3ca 100644 --- a/R/data_to_long.R +++ b/R/data_to_long.R @@ -1,5 +1,7 @@ -#' Reshape (pivot) data from wide to long +#' @title Reshape (pivot) data from wide to long +#' @name data_to_long #' +#' @description #' This function "lengthens" data, increasing the number of rows and decreasing #' the number of columns. This is a dependency-free base-R equivalent of #' `tidyr::pivot_longer()`. @@ -31,6 +33,8 @@ #' with `tidyr::pivot_longer()`. If both `select` and `cols` are provided, `cols` #' is used. #' +#' @inherit data_rename seealso +#' #' @details #' Reshaping data into long format usually means that the input data frame is #' in _wide_ format, where multiple measurements taken on the same subject are @@ -58,7 +62,7 @@ #' @return If a tibble was provided as input, `reshape_longer()` also returns a #' tibble. Otherwise, it returns a data frame. #' -#' @examplesIf requireNamespace("psych") && requireNamespace("tidyr") +#' @examplesIf all(insight::check_if_installed(c("psych", "tidyr"), quietly = TRUE)) #' wide_data <- setNames( #' data.frame(replicate(2, rnorm(8))), #' c("Time1", "Time2") @@ -122,7 +126,6 @@ #' values_to = "count" #' ) #' head(even_longer_data) -#' @inherit data_rename #' @export data_to_long <- function(data, select = "all", diff --git a/R/select_nse.R b/R/select_nse.R index a085a4ce3..766809b6c 100644 --- a/R/select_nse.R +++ b/R/select_nse.R @@ -199,12 +199,11 @@ # small helper, to avoid duplicated code .action_if_not_found <- function( - x, - columns, - matches, - verbose, - ifnotfound -) { + x, + columns, + matches, + verbose, + ifnotfound) { msg <- paste0( "Following variable(s) were not found: ", toString(x[is.na(matches)]) diff --git a/man/adjust.Rd b/man/adjust.Rd index c47606ce0..313402b90 100644 --- a/man/adjust.Rd +++ b/man/adjust.Rd @@ -124,7 +124,7 @@ Note that a regular correlation between two "adjusted" variables is equivalent to the partial correlation between them. } \examples{ -\dontshow{if (require("bayestestR", quietly = TRUE) && require("rstanarm", quietly = TRUE) && require("gamm4", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (all(insight::check_if_installed(c("bayestestR", "rstanarm", "gamm4"), quietly = TRUE))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} adjusted_all <- adjust(attitude) head(adjusted_all) adjusted_one <- adjust(attitude, effect = "complaints", select = "rating") @@ -137,7 +137,7 @@ adjust(attitude, effect = "complaints_LMH", select = "rating", multilevel = TRUE } # Generate data -data <- simulate_correlation(n = 100, r = 0.7) +data <- bayestestR::simulate_correlation(n = 100, r = 0.7) data$V2 <- (5 * data$V2) + 20 # Add intercept # Adjust 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 8d99fb54f..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 diff --git a/man/data_to_long.Rd b/man/data_to_long.Rd index 36a474b83..4a05e735b 100644 --- a/man/data_to_long.Rd +++ b/man/data_to_long.Rd @@ -155,7 +155,7 @@ names, that identify the source of the gathered values, stored in one or more new columns (\code{names_to}). } \examples{ -\dontshow{if (requireNamespace("psych") && requireNamespace("tidyr")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (all(insight::check_if_installed(c("psych", "tidyr"), quietly = TRUE))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} wide_data <- setNames( data.frame(replicate(2, rnorm(8))), c("Time1", "Time2") diff --git a/man/rescale.Rd b/man/rescale.Rd index f163a6e8c..de70bfecf 100644 --- a/man/rescale.Rd +++ b/man/rescale.Rd @@ -132,10 +132,6 @@ A rescaled object. Rescale variables to a new range. Can also be used to reverse-score variables (change the keying/scoring direction), or to expand a range. } -\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'. -} \section{Selection of variables - the \code{select} argument}{ For most functions that have a \code{select} argument (including this function), @@ -182,7 +178,6 @@ rescale(x, add = c(1, 3)) # Specify list of multipliers d <- data.frame(x = 5:15, y = 5:15) rescale(d, multiply = list(x = 1.1, y = 0.5)) - } \seealso{ See \code{\link[=makepredictcall.dw_transformer]{makepredictcall.dw_transformer()}} for use in model formulas. diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 3495a3355..aaf379097 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -221,3 +221,11 @@ test_that("Argument `pattern` is deprecated", { fixed = TRUE ) }) + +test_that("works with lists", { + result <- list(x = 1, y = 2) + expect_error( + data_rename(result, select = names(result), replacement = c("a", "b")), + regex = "must be a data frame" + ) +})