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

change stop_if_not_*() function names to affirmative assert_*() names #230

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions R/aggregate_alignment_loanbook_exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ validate_input_data_aggregate_alignment_loanbook_exposure <- function(data,
matched,
group_vars,
.by = NULL) {
stop_if_not_expected_columns(
assert_expected_columns(
data = data,
cols = c(
group_vars,
Expand All @@ -230,7 +230,7 @@ validate_input_data_aggregate_alignment_loanbook_exposure <- function(data,
)
)

stop_if_not_expected_columns(
assert_expected_columns(
data = matched,
cols = c(
"id_loan",
Expand Down
4 changes: 2 additions & 2 deletions R/analyse.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ analyse <- function(config) {

output_analysis_dir <- get_output_analysis_dir(config)

stop_if_not_length(output_analysis_dir, 1L)
stop_if_not_inherits(output_analysis_dir, "character")
assert_length(output_analysis_dir, 1L)
assert_inherits(output_analysis_dir, "character")

if (dir.exists(output_analysis_dir)) {
ask_for_permission(
Expand Down
32 changes: 16 additions & 16 deletions R/stop_if_.R → R/assert_.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' stop_if_not_inherits
#' assert_inherits
#'
#' @param x an object to be checked
#' @param cls a string defining the expected object class
Expand All @@ -7,7 +7,7 @@
#'
#' @noRd

stop_if_not_inherits <- function(x, cls) {
assert_inherits <- function(x, cls) {
if (isFALSE(inherits(x, cls))) {
arg <- deparse(substitute(x))
cli::cli_abort(
Expand All @@ -21,7 +21,7 @@ stop_if_not_inherits <- function(x, cls) {
}


#' stop_if_not_length
#' assert_length
#'
#' @param x an object to be checked
#' @param len an integer defining the expected length of the object
Expand All @@ -30,7 +30,7 @@ stop_if_not_inherits <- function(x, cls) {
#'
#' @noRd

stop_if_not_length <- function(x, len) {
assert_length <- function(x, len) {
if (length(x) != len) {
arg <- deparse(substitute(x))
cli::cli_abort(
Expand All @@ -44,7 +44,7 @@ stop_if_not_length <- function(x, len) {
}


#' stop_if_dir_not_found
#' assert_dir_exists
#'
#' @param path a string defining the path to a directory
#' @param desc a string describing the type of directory to be added to the
Expand All @@ -54,7 +54,7 @@ stop_if_not_length <- function(x, len) {
#'
#' @noRd

stop_if_dir_not_found <- function(path, desc = NULL) {
assert_dir_exists <- function(path, desc = NULL) {
if (isFALSE(dir.exists(path))) {
if (is.null(desc)) {
msg <- "Directory not found at path:"
Expand All @@ -73,7 +73,7 @@ stop_if_dir_not_found <- function(path, desc = NULL) {
}


#' stop_if_file_not_found
#' assert_file_exists
#'
#' @param path a string defining the path to a file
#' @param desc a string describing the type of file to be added to the error msg
Expand All @@ -82,7 +82,7 @@ stop_if_dir_not_found <- function(path, desc = NULL) {
#'
#' @noRd

stop_if_file_not_found <- function(path, desc = NULL) {
assert_file_exists <- function(path, desc = NULL) {
if (isFALSE(file.exists(path))) {
if (is.null(desc)) {
msg <- "File not found at path:"
Expand All @@ -101,7 +101,7 @@ stop_if_file_not_found <- function(path, desc = NULL) {
}


#' stop_if_sheet_not_found
#' assert_sheet_exists
#'
#' @param sheet a string defining the name of a sheet
#' @param path a string defining the path to a XLS/X file
Expand All @@ -110,7 +110,7 @@ stop_if_file_not_found <- function(path, desc = NULL) {
#'
#' @noRd

stop_if_sheet_not_found <- function(sheet, path) {
assert_sheet_exists <- function(sheet, path) {
if (isFALSE(sheet %in% readxl::excel_sheets(path))) {
cli::cli_abort(
message = c(
Expand All @@ -124,7 +124,7 @@ stop_if_sheet_not_found <- function(sheet, path) {
}


#' stop_if_not_expected_columns
#' assert_expected_columns
#'
#' @param data a data frame to be checked
#' @param cols a vector of expected column names
Expand All @@ -134,7 +134,7 @@ stop_if_sheet_not_found <- function(sheet, path) {
#'
#' @noRd

stop_if_not_expected_columns <- function(data, cols, desc = NULL) {
assert_expected_columns <- function(data, cols, desc = NULL) {
if (isFALSE(all(cols %in% names(data)))) {
if (is.null(desc)) {
msg <- "Data does not contain all of the expected columns."
Expand All @@ -153,7 +153,7 @@ stop_if_not_expected_columns <- function(data, cols, desc = NULL) {
}


#' stop_if_no_files_found
#' assert_any_file_exists
#'
#' @param files a vector of files found
#' @param dir a dir where files were not found
Expand All @@ -165,7 +165,7 @@ stop_if_not_expected_columns <- function(data, cols, desc = NULL) {
#'
#' @noRd

stop_if_no_files_found <- function(files, dir, dir_param, desc) {
assert_any_file_exists <- function(files, dir, dir_param, desc) {
if (length(files) == 0) {
cli::cli_abort(
message = c(
Expand All @@ -179,15 +179,15 @@ stop_if_no_files_found <- function(files, dir, dir_param, desc) {
}


#' stop_if_sector_split_not_one
#' assert_sector_split_is_one
#'
#' @param data a data frame to be checked
#'
#' @return `NULL` invisibly or an error
#'
#' @noRd

stop_if_sector_split_not_one <- function(data) {
assert_sector_split_is_one <- function(data) {
check_sector_split <-
dplyr::summarise(
data,
Expand Down
8 changes: 4 additions & 4 deletions R/calculate_company_alignment_metric.R
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ validate_input_args_calculate_company_tech_deviation <- function(scenario_source

validate_input_data_calculate_company_tech_deviation <- function(data,
technology_direction) {
stop_if_not_expected_columns(
assert_expected_columns(
data = data,
cols = c(
"sector", "technology", "year", "region", "scenario_source", "name_abcd",
Expand All @@ -601,7 +601,7 @@ validate_input_data_calculate_company_tech_deviation <- function(data,
)
)

stop_if_not_expected_columns(
assert_expected_columns(
data = technology_direction,
cols = c(
"scenario_source", "scenario", "sector", "technology", "region",
Expand Down Expand Up @@ -656,7 +656,7 @@ validate_input_args_calculate_company_aggregate_alignment_tms <- function(scenar

validate_input_data_calculate_company_aggregate_alignment_tms <- function(data,
scenario) {
stop_if_not_expected_columns(
assert_expected_columns(
data = data,
cols = c(
"sector", "technology", "year", "region", "scenario_source", "name_abcd",
Expand Down Expand Up @@ -735,7 +735,7 @@ validate_input_args_calculate_company_aggregate_alignment_sda <- function(scenar


validate_input_data_calculate_company_aggregate_alignment_sda <- function(data) {
stop_if_not_expected_columns(
assert_expected_columns(
data = data,
cols = c(
"sector", "year", "region", "scenario_source", "name_abcd",
Expand Down
4 changes: 2 additions & 2 deletions R/helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ apply_sector_split_to_loans <- function(data,

check_and_prepare_by_group <- function(by_group) {
if (!is.null(by_group)) {
stop_if_not_inherits(by_group, "character")
stop_if_not_length(by_group, 1)
assert_inherits(by_group, "character")
assert_length(by_group, 1)
if (by_group == "NULL") {
by_group <- NULL
} else {
Expand Down
56 changes: 28 additions & 28 deletions R/match_loanbooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,47 @@ match_loanbooks <- function(config) {
}

# validate config values----
stop_if_not_length(input_loanbooks_dir, 1L)
stop_if_not_inherits(input_loanbooks_dir, "character")
stop_if_dir_not_found(input_loanbooks_dir, desc = "Input - loanbooks")
assert_length(input_loanbooks_dir, 1L)
assert_inherits(input_loanbooks_dir, "character")
assert_dir_exists(input_loanbooks_dir, desc = "Input - loanbooks")

stop_if_not_length(output_prepare_dir, 1L)
stop_if_not_inherits(output_prepare_dir, "character")
stop_if_dir_not_found(output_prepare_dir, desc = "Output - prepare ABCD")
stop_if_file_not_found(file.path(output_prepare_dir, "abcd_final.csv"), desc = "ABCD final")
assert_length(output_prepare_dir, 1L)
assert_inherits(output_prepare_dir, "character")
assert_dir_exists(output_prepare_dir, desc = "Output - prepare ABCD")
assert_file_exists(file.path(output_prepare_dir, "abcd_final.csv"), desc = "ABCD final")

stop_if_not_length(output_matched_loanbooks_dir, 1L)
stop_if_not_inherits(output_matched_loanbooks_dir, "character")
stop_if_dir_not_found(output_matched_loanbooks_dir, desc = "Output - Matched loanbooks")
assert_length(output_matched_loanbooks_dir, 1L)
assert_inherits(output_matched_loanbooks_dir, "character")
assert_dir_exists(output_matched_loanbooks_dir, desc = "Output - Matched loanbooks")

stop_if_not_length(matching_by_sector, 1L)
stop_if_not_inherits(matching_by_sector, "logical")
assert_length(matching_by_sector, 1L)
assert_inherits(matching_by_sector, "logical")

stop_if_not_length(matching_min_score, 1L)
stop_if_not_inherits(matching_min_score, "numeric")
assert_length(matching_min_score, 1L)
assert_inherits(matching_min_score, "numeric")

stop_if_not_length(matching_method, 1L)
stop_if_not_inherits(matching_method, "character")
assert_length(matching_method, 1L)
assert_inherits(matching_method, "character")

stop_if_not_length(matching_p, 1L)
stop_if_not_inherits(matching_p, "numeric")
assert_length(matching_p, 1L)
assert_inherits(matching_p, "numeric")

# TODO: check for data.frame
# stop_if_not_length(matching_overwrite, 1L)
# stop_if_not_inherits(matching_overwrite, "numeric")
# assert_length(matching_overwrite, 1L)
# assert_inherits(matching_overwrite, "numeric")
#
# TODO: check for join_object
# stop_if_not_length(matching_join_id, 1L)
# stop_if_not_inherits(matching_join_id, "numeric")
# assert_length(matching_join_id, 1L)
# assert_inherits(matching_join_id, "numeric")

stop_if_not_length(matching_use_manual_sector_classification, 1L)
stop_if_not_inherits(matching_use_manual_sector_classification, "logical")
assert_length(matching_use_manual_sector_classification, 1L)
assert_inherits(matching_use_manual_sector_classification, "logical")

# path to manual sector classification only required if boolean TRUE
if (matching_use_manual_sector_classification) {
stop_if_not_length(path_manual_sector_classification, 1L)
stop_if_not_inherits(path_manual_sector_classification, "character")
stop_if_file_not_found(path_manual_sector_classification, desc = "Manual sector classification")
assert_length(path_manual_sector_classification, 1L)
assert_inherits(path_manual_sector_classification, "character")
assert_file_exists(path_manual_sector_classification, desc = "Manual sector classification")
}

# load data----
Expand All @@ -114,7 +114,7 @@ match_loanbooks <- function(config) {

## load raw loan books----
list_raw <- list.files(path = input_loanbooks_dir, pattern = "[.]csv$")
stop_if_no_files_found(list_raw, input_loanbooks_dir, "dir_input", "raw loan book CSVs")
assert_any_file_exists(list_raw, input_loanbooks_dir, "dir_input", "raw loan book CSVs")

raw_lbk <- readr::read_csv(
file = file.path(input_loanbooks_dir, list_raw),
Expand Down
Loading