diff --git a/R/verify-variables.R b/R/verify-variables.R index 2ec0fc6..39c0f02 100644 --- a/R/verify-variables.R +++ b/R/verify-variables.R @@ -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()) { 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)) } diff --git a/man/verify_required_variables.Rd b/man/verify_required_variables.Rd index 6be06f6..3360acb 100644 --- a/man/verify_required_variables.Rd +++ b/man/verify_required_variables.Rd @@ -4,17 +4,19 @@ \alias{verify_required_variables} \title{Verify that the dataset has the required variables for the algorithm.} \usage{ -verify_required_variables(data, register) +verify_required_variables(data, register, call = rlang::caller_env()) } \arguments{ \item{data}{The dataset to check.} \item{register}{The abbreviation of the register name. See list of abbreviations in \code{\link[=get_register_abbrev]{get_register_abbrev()}}.} + +\item{call}{The environment where the function is called, so that the error +traceback gives a more meaningful location.} } \value{ -Either TRUE if the verification passes, or a character string if -there is an error. +Either TRUE if the verification passes, or an error. } \description{ Use this function inside another function within an \code{if} condition to provide an @@ -24,7 +26,7 @@ occurs, rather than within this function. } \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(register_data$bef, "bef") +verify_required_variables(register_data$bef, "lpr_adm") } \keyword{internal} diff --git a/tests/testthat/test-verify-variables.R b/tests/testthat/test-verify-variables.R index 5673c85..9dbaf41 100644 --- a/tests/testthat/test-verify-variables.R +++ b/tests/testthat/test-verify-variables.R @@ -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")) })