From f6bec45465e7024ef52cd8ae6b2853ff3fd7b6dd Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 09:00:34 +0100 Subject: [PATCH 01/11] `data_rename()`: "Error: Following variable(s) were not found" in version '0.13.0.17' Fixes #571 --- R/data_rename.R | 24 +++++++++++++----------- tests/testthat/test-data_rename.R | 10 ++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index eb2415d67..f1f158bcc 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -103,17 +103,19 @@ 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 - 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 - this only works for data frames + 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" + ) + } # Forbid partially named "select", # Ex: if select = c("foo" = "Species", "Sepal.Length") then the 2nd name and diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 3495a3355..8ee09c0ab 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -221,3 +221,13 @@ test_that("Argument `pattern` is deprecated", { fixed = TRUE ) }) + +test_that("works with lists", { + result <- list( + 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_identical(names(out), new_names) +}) From ec33a738f13f339814679eda471ecd68482236d8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 09:01:08 +0100 Subject: [PATCH 02/11] version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index dffcdfc3a..3935073b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.13.0.17 +Version: 0.13.0.18 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531")), From 7102682e6fff475f816bb6b91fadd25677ad5aa4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 09:02:45 +0100 Subject: [PATCH 03/11] comment --- R/data_rename.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/data_rename.R b/R/data_rename.R index f1f158bcc..282aaeff4 100644 --- a/R/data_rename.R +++ b/R/data_rename.R @@ -103,7 +103,7 @@ 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 only works for data frames + # 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, From 11036bb3b6fa6075daaa24c023d37c90efef89c4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 10:01:32 +0100 Subject: [PATCH 04/11] styler, lintr --- R/select_nse.R | 11 +++++------ tests/testthat/test-data_rename.R | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) 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/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 8ee09c0ab..bcaa4e161 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -229,5 +229,5 @@ test_that("works with lists", { ) new_names <- c("setosa, a", "versicolor, a") out <- data_rename(result, select = names(result), replacement = new_names) - expect_identical(names(out), new_names) + expect_named(out, new_names) }) From 7ee80ca86540a21bb425138d18173a0d94dd4488 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 10:13:42 +0100 Subject: [PATCH 05/11] docs --- R/data_rename.R | 5 +++++ R/data_rescale.R | 4 +++- R/data_to_long.R | 4 +++- man/data_rename.Rd | 4 ++++ man/rescale.Rd | 4 ++++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/R/data_rename.R b/R/data_rename.R index 282aaeff4..28fd61704 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 @@ -47,6 +48,10 @@ #' `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 diff --git a/R/data_rescale.R b/R/data_rescale.R index 5707e38f0..ad2742497 100644 --- a/R/data_rescale.R +++ b/R/data_rescale.R @@ -1,5 +1,7 @@ -#' 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. #' diff --git a/R/data_to_long.R b/R/data_to_long.R index deffcc0cb..01d41a81c 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()`. diff --git a/man/data_rename.Rd b/man/data_rename.Rd index 8d99fb54f..a2cd83703 100644 --- a/man/data_rename.Rd +++ b/man/data_rename.Rd @@ -114,6 +114,10 @@ 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 f163a6e8c..0744ca3ba 100644 --- a/man/rescale.Rd +++ b/man/rescale.Rd @@ -135,6 +135,10 @@ 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}{ From 094190c5ed17841cffaf5e6f194221b615fc7711 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 15:58:57 +0100 Subject: [PATCH 06/11] 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" + ) }) From e154bf696759c69bf418e10e9e26f814494d639f Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 16:13:54 +0100 Subject: [PATCH 07/11] Update tests/testthat/test-data_rename.R Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> --- tests/testthat/test-data_rename.R | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/testthat/test-data_rename.R b/tests/testthat/test-data_rename.R index 31dda49b3..aaf379097 100644 --- a/tests/testthat/test-data_rename.R +++ b/tests/testthat/test-data_rename.R @@ -223,10 +223,7 @@ test_that("Argument `pattern` is deprecated", { }) test_that("works with lists", { - result <- list( - setosa.a = structure(data.frame(1)), - versicolor.a = structure(data.frame(2)) - ) + result <- list(x = 1, y = 2) expect_error( data_rename(result, select = names(result), replacement = c("a", "b")), regex = "must be a data frame" From 6f3aacf494a3b9a3d728316589952a9b57520b35 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 16:16:08 +0100 Subject: [PATCH 08/11] fix docs --- R/data_rescale.R | 14 +++++--------- man/rescale.Rd | 5 ----- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/R/data_rescale.R b/R/data_rescale.R index ad2742497..acfc08959 100644 --- a/R/data_rescale.R +++ b/R/data_rescale.R @@ -8,7 +8,6 @@ #' @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. @@ -29,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)) @@ -64,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/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. From 06155c792b429a81e269ec106a62e5cbad8e956d Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 16:18:43 +0100 Subject: [PATCH 09/11] fix --- R/data_rotate.R | 3 ++- R/data_to_long.R | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 01d41a81c..44a9ece5a 100644 --- a/R/data_to_long.R +++ b/R/data_to_long.R @@ -33,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 @@ -124,7 +126,6 @@ #' values_to = "count" #' ) #' head(even_longer_data) -#' @inherit data_rename #' @export data_to_long <- function(data, select = "all", From c7c7c8ad1555f8e06419849e19f9391f5f01de41 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 16:22:54 +0100 Subject: [PATCH 10/11] shorter if-condition --- R/adjust.R | 2 +- R/data_to_long.R | 2 +- man/adjust.Rd | 2 +- man/data_to_long.Rd | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index 9cd5805e9..8c1b1d2ac 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") diff --git a/R/data_to_long.R b/R/data_to_long.R index 44a9ece5a..c44a8d3ca 100644 --- a/R/data_to_long.R +++ b/R/data_to_long.R @@ -62,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") diff --git a/man/adjust.Rd b/man/adjust.Rd index c47606ce0..c1358a31d 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") 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") From aa3546268bdb8b219be01b142d190ed2f7dce429 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 13 Dec 2024 16:42:56 +0100 Subject: [PATCH 11/11] fix --- R/adjust.R | 2 +- man/adjust.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/adjust.R b/R/adjust.R index 8c1b1d2ac..f4cf44f0a 100644 --- a/R/adjust.R +++ b/R/adjust.R @@ -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/man/adjust.Rd b/man/adjust.Rd index c1358a31d..313402b90 100644 --- a/man/adjust.Rd +++ b/man/adjust.Rd @@ -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