Skip to content

Commit

Permalink
update ard_moodtest function to accept multiple variables (#90)
Browse files Browse the repository at this point in the history
update `ard_moodtest()` function to accept multiple variables

closes #89 


--------------------------------------------------------------------------------

Pre-review Checklist (if item does not apply, mark is as complete)
- [ ] **All** GitHub Action workflows pass with a ✅
- [ ] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [ ] If a bug was fixed, a unit test was added.
- [ ] Code coverage is suitable for any new functions/features
(generally, 100% coverage for new code): `devtools::test_coverage()`
- [ ] Request a reviewer

Reviewer Checklist (if item does not apply, mark is as complete)

- [ ] If a bug was fixed, a unit test was added.
- [ ] Run `pkgdown::build_site()`. Check the R console for errors, and
review the rendered website.
- [ ] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`

When the branch is ready to be merged:
- [ ] Update `NEWS.md` with the changes from this pull request under the
heading "`# cards (development version)`". If there is an issue
associated with the pull request, reference it in parentheses at the end
update (see `NEWS.md` for examples).
- [ ] **All** GitHub Action workflows pass with a ✅
- [ ] Approve Pull Request
- [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
  • Loading branch information
ayogasekaram authored Mar 15, 2024
1 parent 1f5536e commit dd87d7c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
45 changes: 28 additions & 17 deletions R/ard_moodtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
#'
Expand Down
9 changes: 5 additions & 4 deletions man/ard_moodtest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-ard_moodtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
})

0 comments on commit dd87d7c

Please sign in to comment.