-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding
ard_total_n.survey.design()
S3 method (#199)
**What changes are proposed in this pull request?** * Bug fix in `ard_categorical.survey.design()` where all unweighted statistics were returned, even in the case where they were explicitly not requested. * Added S3 method `ard_total_n.survey.design()` which returns an ARD with both the survey-weighted and unweighted total sample size. -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [x] **All** GitHub Action workflows pass with a ✅ - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [x] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [x] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [x] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom", reference_pkg = "cardx")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom"", reference_pkg = "cardx"))` - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [x] If a bug was fixed, a unit test was added. - [x] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [x] Update `NEWS.md` with the changes from this pull request under the heading "`# cardx (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). - [x] **All** GitHub Action workflows pass with a ✅ - [x] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
- Loading branch information
Showing
12 changed files
with
153 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#' ARD Total N | ||
#' | ||
#' Returns the total N for a survey object. | ||
#' The placeholder variable name returned in the object is `"..ard_total_n.."` | ||
#' | ||
#' @inheritParams ard_dichotomous.survey.design | ||
#' @inheritParams rlang::args_dots_empty | ||
#' | ||
#' @return an ARD data frame of class 'card' | ||
#' @export | ||
#' | ||
#' @examplesIf cardx:::is_pkg_installed("survey", reference_pkg = "cardx") | ||
#' svy_titanic <- survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) | ||
#' | ||
#' ard_total_n(svy_titanic) | ||
ard_total_n.survey.design <- function(data, ...) { | ||
# process inputs ------------------------------------------------------------- | ||
set_cli_abort_call() | ||
check_dots_empty() | ||
|
||
# calculate total N ---------------------------------------------------------- | ||
data$variables <- | ||
data$variables |> | ||
dplyr::mutate(..ard_total_n.. = TRUE) | ||
|
||
data |> | ||
ard_dichotomous( | ||
variables = "..ard_total_n..", | ||
statistic = list(..ard_total_n.. = c("N", "N_unweighted")) | ||
) |> | ||
dplyr::mutate(context = "total_n") |> | ||
dplyr::select(-cards::all_ard_variables("levels")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# ard_total_n.survey.design() works | ||
|
||
Code | ||
ard_total_n(survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~ | ||
Freq)) | ||
Message | ||
{cards} data frame: 2 x 8 | ||
Output | ||
variable context stat_name stat_label stat fmt_fn | ||
1 ..ard_total_n.. total_n N N 2201 <fn> | ||
2 ..ard_total_n.. total_n N_unweighted Unweight… 32 <fn> | ||
Message | ||
i 2 more variables: warning, error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
skip_if_not(is_pkg_installed("survey", reference_pkg = "cardx")) | ||
|
||
test_that("ard_total_n.survey.design() works", { | ||
expect_snapshot( | ||
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |> | ||
ard_total_n() | ||
) | ||
}) | ||
|
||
test_that("ard_total_n.survey.design() follows ard structure", { | ||
expect_silent( | ||
survey::svydesign(~1, data = as.data.frame(Titanic), weights = ~Freq) |> | ||
ard_total_n() |> | ||
cards::check_ard_structure(method = FALSE) | ||
) | ||
}) |