Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for protected names in ard_categorical.survey.design() #182

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions R/ard_categorical.survey.design.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/ard_categorical.survey.design.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@
! Column "Class" is all missing and cannot be tabulated.
i Only columns of class <logical> and <factor> can be tabulated when all values are missing.

# ard_categorical.survey.design(by) messages about protected names

Code
ard_categorical(svy_mtcars, by = variable, variables = gear)
Condition
Error in `ard_categorical()`:
! The `by` argument cannot include variables named "variable" and "variable_level".

26 changes: 26 additions & 0 deletions tests/testthat/test-ard_categorical.survey.design.R
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,29 @@ 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_mtcars <-
survey::svydesign(
ids = ~1,
data = mtcars |>
dplyr::mutate(
variable = am,
variable_level = cyl,
by = am,
by_level = cyl
),
weights = ~1
)

expect_snapshot(
error = TRUE,
ard_categorical(svy_mtcars, by = variable, variables = gear)
)

expect_error(
ard_categorical(svy_mtcars, by = variable_level, variables = gear),
'The `by` argument cannot include variables named "variable" and "variable_level".'
)
})
Loading