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

fix: 🐛 verify function actually throws error #128

Merged
merged 9 commits into from
Aug 16, 2024
32 changes: 20 additions & 12 deletions R/verify-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@
#' occurs, rather than within this function.
#'
#' @param data The dataset to check.
#' @param call The environment where the function is called, so that the error
#' traceback gives a more meaningful location.
#' @inheritParams get_required_variables
#'
#' @return Either TRUE if the verification passes, or a character string if
#' there is an error.
#' @return Either TRUE if the verification passes, or an error.
#' @keywords internal
#'
#' @examples
#' # TODO: Replace with simulated data.
#' example_bef_data <- tibble::tibble(pnr = 1, koen = 1, foed_dato = 1)
#' osdc:::verify_required_variables(example_bef_data, "bef")
verify_required_variables <- function(data, register) {
#' verify_required_variables(register_data$bef, "bef")
#' verify_required_variables(register_data$lpr_adm, "lpr_adm")
verify_required_variables <- function(data, register, call = rlang::caller_env()) {
lwjohnst86 marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_choice(register, get_register_abbrev())
expected_variables <- sort(get_required_variables(register))
actual_variables <- sort(colnames(data))

# TODO: Consider using/looking into rlang::try_fetch() to provide contextual error messages.
expected_variables <- get_required_variables(register)

actual_variables <- colnames(data)

checkmate::check_names(
if (!checkmate::test_names(
x = actual_variables,
must.include = expected_variables
)
)) {
cli::cli_abort(
c(
"This function needs specific variables from the {.val {register}} register.",
"i" = "Variables required: {.val {expected_variables}}",
"x" = "Variables found: {.val {actual_variables}}"
),
call = call
)
}
return(invisible(TRUE))
}
12 changes: 7 additions & 5 deletions man/verify_required_variables.Rd

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

3 changes: 1 addition & 2 deletions tests/testthat/test-verify-variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ test_that("the required variables are present in the dataset", {
# When some of the variables are the required variables
expect_true(verify_required_variables(bef_complete_extra, "bef"))

# When it is a character output, it is a fail
expect_type(verify_required_variables(bef_incomplete, "bef"), "character")
expect_error(verify_required_variables(bef_incomplete, "bef"))
})


Expand Down
Loading