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

replace custom stop and warn messages with cli versions #68

Merged
merged 6 commits into from
Aug 19, 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
15 changes: 7 additions & 8 deletions R/helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ apply_sector_split_to_loans <- function(data,
)

if (nrow(unique_companies_pre_split) != nrow(unique_companies_post_split)) {
warning(
glue::glue(
"Applying the sector split has lead to changes in the number of unique
companies covered in the analysis. Prior to the split, there were
{nrow(unique_companies_pre_split)} unique companies. After the split,
there are {nrow(unique_companies_post_split)} unique companies."
)
)
n_pre <- nrow(unique_companies_pre_split)
n_post <- nrow(unique_companies_post_split)
cli::cli_warn(c(
"!" = "Applying the sector split has lead to changes in the number of unique companies covered in the analysis.",
"i" = "Prior to the split, there {?was/were} {.strong {n_pre}} unique compan{?y/ies}.",
"i" = "After the split, there {?is/are} {.strong {n_post}} unique compan{?y/ies}."
))
}

data
Expand Down
31 changes: 3 additions & 28 deletions R/prepare_sector_split.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,7 @@ prepare_sector_split <- function() {
)

### check that the sum of the sector split of each company is 1----
check_sector_split_all_companies <- sector_split_all_companies %>%
dplyr::summarise(
sum_share = sum(.data[["sector_split"]], na.rm = TRUE),
.by = "company_id"
)

if (any(round(check_sector_split_all_companies$sum_share, 3) != 1)) {
stop("sector_split_all_companies contains companies for which the sum of the sector split deviates from 1")
}
stop_if_sector_split_not_one(sector_split_all_companies)

## calculate primary energy-based sector split for energy sectors----
# keep only companies that are active in multiple energy sectors
Expand Down Expand Up @@ -309,15 +301,7 @@ prepare_sector_split <- function() {
dplyr::filter(.data$company_id %in% company_ids_primary_energy_split)

### check that the sum of the primary energy based sector split of each company is 1----
check_sector_split_multi_energy_companies <- sector_split_multi_energy_companies %>%
dplyr::summarise(
sum_share = sum(.data[["sector_split"]], na.rm = TRUE),
.by = "company_id"
)

if (any(round(check_sector_split_multi_energy_companies$sum_share, 3) != 1)) {
stop("sector_split_multi_energy_companies contains companies for which the sum of the sector split deviates from 1")
}
stop_if_sector_split_not_one(sector_split_multi_energy_companies)

## combine the sector splits----
# we want to use the plain equal weights split for companies that do not operate in more than one energy sector
Expand All @@ -344,16 +328,7 @@ prepare_sector_split <- function() {
)

### check that the sum of the combined sector split of each company is 1----
check_sector_split_all_companies_final <- sector_split_all_companies_final %>%
dplyr::summarise(
sum_share = sum(.data$sector_split, na.rm = TRUE),
.by = "company_id"
)

if (any(round(check_sector_split_all_companies_final$sum_share, 3) != 1)) {
stop("sector_split_all_companies_final contains companies for which the sum of the sector split deviates from 1")
}

stop_if_sector_split_not_one(sector_split_all_companies_final)

## write output----
sector_split_multi_energy_companies %>%
Expand Down
4 changes: 1 addition & 3 deletions R/run_calculate_loanbook_coverage.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ run_calculate_loanbook_coverage <- function() {

## read matched prioritized loan books----
list_matched_prioritized <- list.files(path = dir_matched, pattern = "^matched_prio_.*csv$")
stop_if_no_files_found(list_matched_prioritized, dir_matched, "dir_matched", "matched prioritized loan book CSVs")

if (length(list_matched_prioritized) == 0) {
stop(glue::glue("No matched prioritized loan book csvs found in {dir_matched}. Please check your project setup!"))
}

matched_prioritized <- readr::read_csv(
file = file.path(dir_matched, list_matched_prioritized),
Expand Down
20 changes: 7 additions & 13 deletions R/run_calculate_match_success_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ run_calculate_match_success_rate <- function() {

## load raw loan books----
list_raw <- list.files(dir_raw)[grepl("csv$", list.files(dir_raw))]

if (length(list_raw) == 0) {
stop(glue::glue("No raw loan book csvs found in {dir_raw}. Please check your project setup!"))
}
stop_if_no_files_found(list_raw, dir_raw, "dir_raw", "raw loan book CSVs")

raw_lbk <- readr::read_csv(
file = file.path(dir_raw, list_raw),
Expand All @@ -73,10 +70,7 @@ run_calculate_match_success_rate <- function() {

## load matched prioritized loan books----
list_matched_prioritized <- list.files(dir_matched)[grepl("^matched_prio_.*csv$", list.files(dir_matched))]

if (length(list_matched_prioritized) == 0) {
stop(glue::glue("No matched prioritized loan book csvs found in {dir_matched}. Please check your project setup!"))
}
stop_if_no_files_found(list_matched_prioritized, dir_matched, "dir_matched", "matched prioritized loan book CSVs")

matched_prioritized <- readr::read_csv(
file = file.path(dir_matched, list_matched_prioritized),
Expand All @@ -95,11 +89,11 @@ run_calculate_match_success_rate <- function() {
sector_classifications_used <- unique(raw_lbk$sector_classification_system)

if (length(sector_classifications_used) != 1) {
stop(
glue::glue(
"Number of sector classification systems across all loan books must be 1.
Your raw loan books use {length(sector_classifications_used)} different
types of sector classifications. Please choose one!"
cli::cli_abort(
message = c(
"x" = "Number of sector classification systems across all loan books is > 1.",
"i" = "You can only use one sector classification at the same time.",
"i" = "Your raw loan books use {length(sector_classifications_used)} different types of sector classifications."
)
)
}
Expand Down
23 changes: 16 additions & 7 deletions R/run_match_prioritize.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ run_match_prioritize <- function() {
!inherits(match_prio_priority, "formula") &
!inherits(match_prio_priority, "function")
) {
stop(
glue::glue(
"Argument match_prio_priority must be of one of: a character vector, a
function, or a quosure-style lambda function. Your input is of class
{class(match_prio_priority)}. Please check your input."
)
valid_types <- c(
"a character vector",
"a function",
"a quosure-style lambda function"
)
cli::cli_abort(c(
"x" = paste0(
"Argument {.arg match_prio_priority} must be of one of: {.or {valid_types}}, ",
"not {.cls {class(match_prio_priority)}}."
),
"i" = "Check the {.val match_prioritize:priority} parameter set in your {.file config.yml}."
))
}
}

Expand All @@ -40,7 +45,11 @@ run_match_prioritize <- function() {
list_matched_manual <- list.files(path = dir_matched, pattern = "^matched_lbk_.*_manual[.]csv$")

if (length(list_matched_manual) == 0) {
stop(glue::glue("No manually matched loan book csvs found in {dir_matched}. Please check your project setup!"))
cli::cli_abort(c(
"x" = "No manually matched loan book csvs were found.",
"i" = "No files matching the pattern {.code ^matched_lbk_.*_manual[.]csv$} were found in {.path {dir_matched}}. Have you done the manual matching process and named the edited CSVs properly?",
"i" = "If {.path {dir_matched}} is not the correct directory, check the {.val dir_matched} parameter set in your {.file config.yml}."
))
}

matched_lbk_manual <- readr::read_csv(
Expand Down
5 changes: 1 addition & 4 deletions R/run_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ run_matching <- function() {

## load raw loan books----
list_raw <- list.files(path = dir_raw, pattern = "[.]csv$")

if (length(list_raw) == 0) {
stop(glue::glue("No raw loan book csvs found in {dir_raw}. Please check your project setup!"))
}
stop_if_no_files_found(list_raw, dir_raw, "dir_raw", "raw loan book CSVs")

raw_lbk <- readr::read_csv(
file = file.path(dir_raw, list_raw),
Expand Down
56 changes: 56 additions & 0 deletions R/stop_if_.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,59 @@ stop_if_not_expected_columns <- function(data, cols, desc = NULL) {
)
}
}


#' stop_if_no_files_found
#'
#' @param files a vector of files found
#' @param dir a dir where files were not found
#' @param dir_param a string identifying the paramter name in the config that
#' defines the directory
#' @param desc a string describing the type of files expected to be found
#'
#' @return `NULL` invisibly or an error
#'
#' @noRd

stop_if_no_files_found <- function(files, dir, dir_param, desc) {
if (length(files) == 0) {
cli::cli_abort(
message = c(
"x" = "No {desc} found.",
"i" = "Directory searched: {.path {dir}}",
"i" = "Check the {.arg {dir_param}} parameter in your {.file config.yml}."
),
call = rlang::caller_env()
)
}
}


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

stop_if_sector_split_not_one <- function(data) {
check_sector_split <-
dplyr::summarise(
data,
sum_share = sum(.data[["sector_split"]], na.rm = TRUE),
.by = "company_id"
)

if (any(round(check_sector_split$sum_share, 3) != 1)) {
obj_name <- deparse(substitute(data))
msg <- "{.arg {obj_name}} contains companies for which the sum of the sector split deviates from 1"
cli::cli_abort(
message = c(
"x" = msg,
"i" = "Check the sector split set in your {.file config.yml}."
),
call = rlang::caller_env()
)
}
}
Loading