From 36deacc3b7249060b07a9017c23116f13726dc30 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Mon, 8 Jul 2024 08:06:55 -0700 Subject: [PATCH] progress --- R/ard_categorical.survey.design.R | 9 +++++++++ .../_snaps/ard_categorical.survey.design.md | 8 ++++++++ .../test-ard_categorical.survey.design.R | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/R/ard_categorical.survey.design.R b/R/ard_categorical.survey.design.R index 1827970ec..dcecc8eee 100644 --- a/R/ard_categorical.survey.design.R +++ b/R/ard_categorical.survey.design.R @@ -117,6 +117,15 @@ ard_categorical.survey.design <- function(data, } ) + # return note about column names that result in errors ----------------------- + if (any(by %in% c("variable", "variable_level"))) { + cli::cli_abort( + "The {.arg by} argument cannot include variables named {.val {c('variable', 'variable_level')}}", + call = get_cli_abort_call() + ) + } + + # calculate counts ----------------------------------------------------------- # this tabulation accounts for unobserved combinations svytable_counts <- .svytable_counts(data, variables, by, denominator) diff --git a/tests/testthat/_snaps/ard_categorical.survey.design.md b/tests/testthat/_snaps/ard_categorical.survey.design.md index c72bebd09..f1a448c8e 100644 --- a/tests/testthat/_snaps/ard_categorical.survey.design.md +++ b/tests/testthat/_snaps/ard_categorical.survey.design.md @@ -28,3 +28,11 @@ ! Column "Class" is all missing and cannot be tabulated. i Only columns of class and can be tabulated when all values are missing. +# ard_categorical.survey.design(by) messages about protected names + + Code + ard_categorical(svy_trial, by = variable, variables = stage) + Condition + Error in `ard_categorical()`: + ! The `by` argument cannot include variables named "variable" and "variable_level" + diff --git a/tests/testthat/test-ard_categorical.survey.design.R b/tests/testthat/test-ard_categorical.survey.design.R index 7ba6333e9..c67c79c7f 100644 --- a/tests/testthat/test-ard_categorical.survey.design.R +++ b/tests/testthat/test-ard_categorical.survey.design.R @@ -1172,3 +1172,19 @@ test_that("ard_categorical.survey.design() works with variables with only 1 leve ) expect_invisible(cards::check_ard_structure(ard_svy_cat_cell, method = FALSE)) }) + + +test_that("ard_categorical.survey.design(by) messages about protected names", { + svy_trial <- + survey::svydesign( + ids = ~1, + data = gtsummary::trial |> dplyr::mutate(variable = trt, variable_level = grade), + weights = ~1 + ) + + expect_snapshot( + error = TRUE, + ard_categorical(svy_trial, by = variable, variables = stage) + ) + +})