From 98e44b137618a257c4e23c240fbaeb3b56821c21 Mon Sep 17 00:00:00 2001 From: ayogasekaram Date: Thu, 14 Mar 2024 19:18:41 +0000 Subject: [PATCH] update `ard_moodtest` function to accept multiple variables --- R/ard_moodtest.R | 45 +++++++++++++++++++----------- man/ard_moodtest.Rd | 9 +++--- tests/testthat/test-ard_moodtest.R | 10 +++++++ 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/R/ard_moodtest.R b/R/ard_moodtest.R index b772b1c43..dddb82128 100644 --- a/R/ard_moodtest.R +++ b/R/ard_moodtest.R @@ -8,8 +8,9 @@ #' a data frame. See below for details. #' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr #' column name to compare by. -#' @param variable ([`tidy-select`][dplyr::dplyr_tidy_select])\cr -#' column name to be compared. +#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr +#' column name to be compared. Independent tests will +#' be run for each variable. #' @param ... arguments passed to `mood.test(...)` #' #' @return ARD data frame @@ -23,32 +24,42 @@ #' #' @examplesIf cards::is_pkg_installed("broom", reference_pkg = "cardx") #' cards::ADSL |> -#' ard_moodtest(by = "SEX", variable = "AGE") -ard_moodtest <- function(data, by, variable, ...) { +#' ard_moodtest(by = "SEX", variables = "AGE") +ard_moodtest <- function(data, by, variables, ...) { # check installed packages --------------------------------------------------- cards::check_pkg_installed("broom", reference_pkg = "cardx") # check/process inputs ------------------------------------------------------- check_not_missing(data) - check_not_missing(variable) + check_not_missing(variables) check_not_missing(by) check_data_frame(data) data <- dplyr::ungroup(data) - cards::process_selectors(data, by = {{ by }}, variable = {{ variable }}) + cards::process_selectors(data, by = {{ by }}, variables = {{ variables }}) check_scalar(by) - check_scalar(variable) + + # if no variables selected, return empty tibble ------------------------------ + if (is_empty(variables)) { + return(dplyr::tibble()) + } # build ARD ------------------------------------------------------------------ - .format_moodtest_results( - by = by, - variable = variable, - lst_tidy = - cards::eval_capture_conditions( - stats::mood.test(data[[variable]] ~ data[[by]], ...) |> - broom::tidy() - ), - ... - ) + lapply( + variables, + function(variable) { + .format_moodtest_results( + by = by, + variable = variable, + lst_tidy = + cards::eval_capture_conditions( + stats::mood.test(data[[variable]] ~ data[[by]], ...) |> + broom::tidy() + ), + ... + ) + } + ) |> + dplyr::bind_rows() } #' Convert mood test results to ARD #' diff --git a/man/ard_moodtest.Rd b/man/ard_moodtest.Rd index f23ca7dd0..8af20f89f 100644 --- a/man/ard_moodtest.Rd +++ b/man/ard_moodtest.Rd @@ -4,7 +4,7 @@ \alias{ard_moodtest} \title{ARD Mood Test} \usage{ -ard_moodtest(data, by, variable, ...) +ard_moodtest(data, by, variables, ...) } \arguments{ \item{data}{(\code{data.frame})\cr @@ -13,8 +13,9 @@ a data frame. See below for details.} \item{by}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr column name to compare by.} -\item{variable}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr -column name to be compared.} +\item{variables}{(\code{\link[dplyr:dplyr_tidy_select]{tidy-select}})\cr +column name to be compared. Independent tests will +be run for each variable.} \item{...}{arguments passed to \code{mood.test(...)}} } @@ -32,6 +33,6 @@ The data is passed as \code{mood.test(data[[variable]] ~ data[[by]], ...)}. \examples{ \dontshow{if (cards::is_pkg_installed("broom", reference_pkg = "cardx")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} cards::ADSL |> - ard_moodtest(by = "SEX", variable = "AGE") + ard_moodtest(by = "SEX", variables = "AGE") \dontshow{\}) # examplesIf} } diff --git a/tests/testthat/test-ard_moodtest.R b/tests/testthat/test-ard_moodtest.R index 4220fcd91..895c1692c 100644 --- a/tests/testthat/test-ard_moodtest.R +++ b/tests/testthat/test-ard_moodtest.R @@ -24,4 +24,14 @@ test_that("ard_moodtest() works", { ard_moodtest(by = SEX, variable = AGE) |> as.data.frame() ) + + expect_equal( + dplyr::bind_rows( + ard_moodtest, + cards::ADSL |> + ard_moodtest(by = SEX, variable = BMIBL) + ), + cards::ADSL |> + ard_moodtest(by = SEX, variable = c(AGE, BMIBL)) + ) })