From cc99175581c0c2142ec4bb864e6bf3230b55e768 Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Tue, 12 Nov 2024 20:27:15 +0100 Subject: [PATCH 1/7] part 1 --- R/plot_sankey.R | 20 ++++++++------------ R/plot_scatter.R | 15 ++++++++++++--- R/prep_sankey.R | 35 +++++++++++++++++------------------ 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/R/plot_sankey.R b/R/plot_sankey.R index ee0e91c5..9f582d9f 100644 --- a/R/plot_sankey.R +++ b/R/plot_sankey.R @@ -153,12 +153,10 @@ plot_sankey <- function(data, # you convert it as png webshot::webshot(temp_html, path.expand(file_name), vwidth = 1000, vheight = 900) } else { - rlang::abort( - glue::glue( - "In order to save the plot as .png you need to have `phantomjs` - installed. Please run `webshot::install_phantomjs()` if you don't and - try running the function again." - ) + cli::cli_abort( + "In order to save the plot as PNG, you need to have {.pkg phantomjs} + installed. Please run {.run webshot::install_phantomjs()} if you don't + and try running the function again." ) } } @@ -171,11 +169,9 @@ check_plot_sankey <- function(data, crucial_names <- c(group_var, "middle_node", "is_aligned", "loan_size_outstanding") abort_if_missing_names(data, crucial_names) if (!is.logical(capitalise_node_labels)) { - rlang::abort( - c( - "`capitalise_node_labels` must have a logical value.", - x = glue::glue("You provided: {capitalise_node_labels}.") - ) - ) + cli::cli_abort(c( + x = "`capitalise_node_labels` must have a {.cls logical} value.", + i = "capitalise_node_labels` contains the value{?s}: {.val {capitalise_node_labels}}." + )) } } diff --git a/R/plot_scatter.R b/R/plot_scatter.R index 672c95ec..29011da3 100644 --- a/R/plot_scatter.R +++ b/R/plot_scatter.R @@ -217,17 +217,26 @@ check_plot_scatter <- function(data, alignment_limit, cap_outliers, floor_outlie )) if (!is.null(alignment_limit)) { if ((length(alignment_limit) != 1) || (!is.numeric(alignment_limit))) { - rlang::abort("'alignment_limit' must be a numeric value.") + cli::cli_abort(c( + x = "{.arg alignment_limit} must be a {.cls numeric} value.", + i = "{.arg alignment_limit} contains the value{?s}: {.val {alignment_limit}}" + )) } } if (!is.null(cap_outliers)) { if ((length(cap_outliers) != 1) || (!is.numeric(cap_outliers))) { - rlang::abort("'cap_outliers' must be a numeric value.") + cli::cli_abort(c( + x = "{.arg cap_outliers} must be a {.cls numeric} value.", + i = "{.arg cap_outliers} contains the value{?s}: {.val {cap_outliers}}" + )) } } if (!is.null(floor_outliers)) { if ((length(floor_outliers) != 1) || (!is.numeric(floor_outliers))) { - rlang::abort("'floor_outliers' must be a numeric value.") + cli::cli_abort(c( + x = "{.arg floor_outliers} must be a {.cls numeric} value.", + i = "{.arg floor_outliers} contains the value{?s}: {.val {floor_outliers}}" + )) } } } diff --git a/R/prep_sankey.R b/R/prep_sankey.R index 042bfe33..12eff5f2 100644 --- a/R/prep_sankey.R +++ b/R/prep_sankey.R @@ -12,7 +12,7 @@ #' @return data.frame #' #' @rdname plot_sankey -#' +#' #' @noRd prep_sankey <- function(data_alignment, @@ -94,20 +94,20 @@ check_prep_sankey <- function(data_alignment, names_aggergate <- c("region", "year") abort_if_missing_names(data_alignment, c(names_all, names_aggergate)) if (!(region %in% unique(data_alignment$region))) { - rlang::abort(c( - "`region_tms` value not found in `data_alignment` dataset.", - i = glue::glue("Regions in `data_alignment` are: {toString(unique(data_alignment$region))}"), - x = glue::glue("You provided region = {region}.") + cli::cli_abort(c( + x = "{.arg region} value not found in {.var data_alignment} dataset", + i = "{cli::qty(length(unique(data_alignment$region)))}region{?s} in + {.var data_alignment} {?is/are}: + {.val {as.character(unique(data_alignment$region))}}", + i = "the value{?s} in {.arg region} {?is/are}: {.val {region}}" )) } if (!(year %in% unique(data_alignment$year))) { - rlang::abort(c( - "`year` value not found in `data_alignment`.", - i = glue::glue( - "Years in `data_alignment` are: {toString(unique(data_alignment$year))} - " - ), - x = glue::glue("You provided year = {year}.") + cli::cli_abort(c( + x = "{.arg year} value not found in {.var data_alignment} dataset", + i = "{cli::qty(length(unique(data_alignment$year)))}year{?s} in + {.var data_alignment} {?is/are}: {.val {unique(data_alignment$year)}}", + i = "the value{?s} in {.arg year} {?is/are}: {.val {unique(year)}}" )) } abort_if_middle_node_column_not_found(data_alignment, middle_node, env = list(data = substitute(data_alignment))) @@ -120,12 +120,11 @@ abort_if_middle_node_column_not_found <- function(data, name, env = parent.frame .data <- deparse1(substitute(data, env = env)) if (!(name %in% names(data))) { - rlang::abort(c( - glue::glue("Column name you passed as one of the middle nodes not found in {.data}."), - i = glue::glue( - "Column names in `{.data}` are: {toString(names(data))}" - ), - x = glue::glue("You asked to use column named: `{name}`.") + cli::cli_abort(c( + x = "column name you passed as one of the middle nodes not found in {.var {(.data)}}", + i = "{cli::qty(length(names(data)))}column name{?s} in + {.var {(.data)}} {?is/are}: {.val {names(data)}}", + i = "you asked to use column named: {.val {name}}" )) } } From 58f1caebad9962d2ddb3971623141c9e77218b8f Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Tue, 12 Nov 2024 20:45:06 +0100 Subject: [PATCH 2/7] `stopifnot()` to `cli::cli_abort()` --- R/utils.R | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index 727af6c6..e5df6d91 100644 --- a/R/utils.R +++ b/R/utils.R @@ -22,8 +22,19 @@ beautify_scenario_label <- function(label) { #' @noRd abort_if_missing_names <- function(data, expected_names) { - stopifnot(rlang::is_named(data)) - stopifnot(is.character(expected_names)) + if (!rlang::is_named(data)) { + cli::cli_abort( + message = c(x = "{.arg data} must be named"), + .envir = rlang::env_parent() + ) + } + + if (!is.character(expected_names)) { + cli::cli_abort( + message = c(x = "{.arg expected_names} must be of type {.cls character}"), + .envir = rlang::env_parent() + ) + } if (!all(unique(expected_names) %in% names(data))) { missing_names <- sort(setdiff(expected_names, names(data))) From e6c9fdc483eae8f2df7112ee63c410f6859c8e24 Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Tue, 12 Nov 2024 21:19:16 +0100 Subject: [PATCH 3/7] `stop()` to `cli::cli_abort()` --- R/aggregate_alignment_loanbook_exposure.R | 11 +- R/calculate_company_alignment_metric.R | 116 ++++++++++++---------- R/plot_sankey.R | 4 +- R/plot_scatter_alignment_exposure.R | 4 +- R/prep_sankey.R | 4 +- R/prep_scatter.R | 6 +- R/prep_scatter_alignment_exposure.R | 6 +- 7 files changed, 80 insertions(+), 71 deletions(-) diff --git a/R/aggregate_alignment_loanbook_exposure.R b/R/aggregate_alignment_loanbook_exposure.R index 04d06371..69e49e25 100644 --- a/R/aggregate_alignment_loanbook_exposure.R +++ b/R/aggregate_alignment_loanbook_exposure.R @@ -43,7 +43,10 @@ aggregate_alignment_loanbook_exposure <- function(data, if (!is.null(.by)) { if (!inherits(.by, "character")) { - stop(glue::glue("`.by` must be a character vector. Your input is {class(.by)}.")) + cli::cli_abort(c( + x = "{.arg .by} must a {.cls character} vector", + i = "your input is a{.cls {typeof(.by)}}" + )) } group_vars <- c(.by, group_vars) } @@ -259,9 +262,9 @@ validate_input_data_aggregate_alignment_loanbook_exposure <- function(data, ) ) ) { - stop( - "It is not possible to group by the critical columns of the `data` and - `matched` inputs. Please check your .by argument!" + cli::cli_abort( + "It is not possible to group by the critical columns of the {.arg data} + and {.arg matched} inputs. Please check your {.arg .by} argument!" ) } } diff --git a/R/calculate_company_alignment_metric.R b/R/calculate_company_alignment_metric.R index d9867b62..257771ad 100644 --- a/R/calculate_company_alignment_metric.R +++ b/R/calculate_company_alignment_metric.R @@ -513,44 +513,47 @@ validate_input_calculate_company_tech_deviation <- function(data, # consistency checks if (!scenario_source %in% unique(data$scenario_source)) { - stop( - paste0( - "input value of `scenario_source` not found in `data`. You provided ", - scenario_source, ". Available values are: ", - toString(unique(data$scenario_source)) + cli::cli_abort( + message = c( + x = "input value of {.arg scenario_source} not found in {.arg data}", + i = "You provided: {scenario_source}", + i = "Available values are: {unique(data$scenario_source)}" ) ) } if (!any(grepl(pattern = scenario, x = unique(data$metric)))) { - stop( - paste0( - "input value of `scenario` not matched to any sub string in - `data$metric`. You provided ", scenario, ". Available values are: ", - data %>% - dplyr::filter(grepl("target_", .data$metric)) %>% - dplyr::pull(.data$metric) %>% - unique() %>% - gsub(pattern = "target_", replacement = "") %>% - toString() + available_scenarios <- + data %>% + dplyr::filter(grepl("target_", .data$metric)) %>% + dplyr::pull(.data$metric) %>% + unique() %>% + gsub(pattern = "target_", replacement = "") %>% + toString() + + cli::cli_abort( + message = c( + x = "input value of {.arg scenario} not matched to any sub string in {.arg data$metric}", + i = "You provided: {scenario}", + i = "Available values are: {available_scenarios}" ) ) } if (!scenario_source %in% unique(technology_direction$scenario_source)) { - stop( - paste0( - "input value of `scenario_source` not found in `technology_direction` - dataset. You provided ", scenario_source, ". Available values are: ", - toString(unique(technology_direction$scenario_source)) + cli::cli_abort( + message = c( + x = "input value of {.arg scenario_source} not found in {.arg technology_direction}", + i = "You provided: {scenario_source}", + i = "Available values are: {unique(technology_direction$scenario_source)}" ) ) } if (!scenario %in% unique(technology_direction$scenario)) { - stop( - paste0( - "input value of `scenario` not found in `technology_direction` - dataset. You provided ", scenario, ". Available values are: ", - toString(unique(technology_direction$scenario)) + cli::cli_abort( + message = c( + x = "input value of {.arg scenario} not found in {.arg technology_direction}", + i = "You provided: {scenario}", + i = "Available values are: {unique(technology_direction$scenario)}" ) ) } @@ -563,28 +566,28 @@ validate_input_args_calculate_company_tech_deviation <- function(scenario_source bridge_tech, time_frame) { if (!length(scenario_source) == 1) { - stop("Argument scenario_source must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario_source must be of length 1. Please check your input.") } if (!inherits(scenario_source, "character")) { - stop("Argument scenario_source must be of class character. Please check your input.") + cli::cli_abort("Argument scenario_source must be of class character. Please check your input.") } if (!length(scenario) == 1) { - stop("Argument scenario must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario must be of length 1. Please check your input.") } if (!inherits(scenario, "character")) { - stop("Argument scenario must be of class character. Please check your input.") + cli::cli_abort("Argument scenario must be of class character. Please check your input.") } if (!length(bridge_tech) == 1) { - stop("Argument bridge_tech must be of length 1. Please check your input.") + cli::cli_abort("Argument bridge_tech must be of length 1. Please check your input.") } if (!inherits(bridge_tech, "character")) { - stop("Argument bridge_tech must be of class character. Please check your input.") + cli::cli_abort("Argument bridge_tech must be of class character. Please check your input.") } if (!length(time_frame) == 1) { - stop("Argument time_frame must be of length 1. Please check your input.") + cli::cli_abort("Argument time_frame must be of length 1. Please check your input.") } if (!inherits(time_frame, "integer")) { - stop("Argument time_frame must be of class integer Please check your input.") + cli::cli_abort("Argument time_frame must be of class integer Please check your input.") } invisible() @@ -639,16 +642,16 @@ validate_input_calculate_company_aggregate_alignment_tms <- function(data, validate_input_args_calculate_company_aggregate_alignment_tms <- function(scenario_source, scenario) { if (!length(scenario_source) == 1) { - stop("Argument scenario_source must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario_source must be of length 1. Please check your input.") } if (!inherits(scenario_source, "character")) { - stop("Argument scenario_source must be of class character. Please check your input.") + cli::cli_abort("Argument scenario_source must be of class character. Please check your input.") } if (!length(scenario) == 1) { - stop("Argument scenario must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario must be of length 1. Please check your input.") } if (!inherits(scenario, "character")) { - stop("Argument scenario must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario must be of length 1. Please check your input.") } invisible() @@ -671,10 +674,11 @@ validate_input_data_calculate_company_aggregate_alignment_tms <- function(data, check_consistency_calculate_company_aggregate_alignment_tms <- function(data, scenario_source) { if (!scenario_source %in% unique(data$scenario_source)) { - stop( - paste0( - "input value of `scenario_source` not found in `data$scenario_source`. You provided ", - scenario_source, ". Available values are: ", toString(unique(data$scenario_source)) + cli::cli_abort( + message = c( + x = "input value of {.arg scenario_source} not found in {.arg data$scenario_source}", + i = "You provided: {scenario_source}", + i = "Available values are: {unique(data$scenario_source)}" ) ) } @@ -712,22 +716,22 @@ validate_input_args_calculate_company_aggregate_alignment_sda <- function(scenar scenario, time_frame) { if (!length(scenario_source) == 1) { - stop("Argument scenario_source must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario_source must be of length 1. Please check your input.") } if (!inherits(scenario_source, "character")) { - stop("Argument scenario_source must be of class character. Please check your input.") + cli::cli_abort("Argument scenario_source must be of class character. Please check your input.") } if (!length(scenario) == 1) { - stop("Argument scenario must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario must be of length 1. Please check your input.") } if (!inherits(scenario, "character")) { - stop("Argument scenario must be of class character. Please check your input.") + cli::cli_abort("Argument scenario must be of class character. Please check your input.") } if (!length(time_frame) == 1) { - stop("Argument scenario must be of length 1. Please check your input.") + cli::cli_abort("Argument scenario must be of length 1. Please check your input.") } if (!inherits(time_frame, "integer")) { - stop("Argument time_frame must be of class integer. Please check your input.") + cli::cli_abort("Argument time_frame must be of class integer. Please check your input.") } invisible() @@ -750,10 +754,11 @@ check_consistency_calculate_company_aggregate_alignment_sda <- function(data, scenario_source, scenario) { if (!scenario_source %in% unique(data$scenario_source)) { - stop( - paste0( - "input value of `scenario_source` not found in `data$scenario_source`. You provided: ", - scenario_source, ". Available values are: ", toString(unique(data$scenario_source)) + cli::cli_abort( + message = c( + x = "input value of {.arg scenario_source} not found in {.arg data$scenario_source}", + i = "You provided: {scenario_source}", + i = "Available values are: {unique(data$scenario_source)}" ) ) } @@ -763,10 +768,11 @@ check_consistency_calculate_company_aggregate_alignment_sda <- function(data, dplyr::pull(.data$emission_factor_metric) %>% unique() if (!scenario %in% available_scenarios) { - stop( - paste0( - "input value of `scenario` not found in `data$emission_factor_metric`. You provided ", - scenario, ". Available values are: ", toString(available_scenarios) + cli::cli_abort( + message = c( + x = "input value of {.arg scenario} not found in {.arg data$emission_factor_metric}", + i = "You provided: {scenario}", + i = "Available values are: {available_scenarios}" ) ) } diff --git a/R/plot_sankey.R b/R/plot_sankey.R index 9f582d9f..74b9ae76 100644 --- a/R/plot_sankey.R +++ b/R/plot_sankey.R @@ -26,10 +26,10 @@ plot_sankey <- function(data, nodes_order_from_data = FALSE) { if (!is.null(group_var)) { if (!inherits(group_var, "character")) { - stop("group_var must be of class character") + cli::cli_abort("{.arg group_var} must be of class {.cls character}") } if (!length(group_var) == 1) { - stop("group_var must be of length 1") + cli::cli_abort("{.arg group_var} must be of length 1") } } else { data <- data %>% diff --git a/R/plot_scatter_alignment_exposure.R b/R/plot_scatter_alignment_exposure.R index ad2db88e..337aeabf 100644 --- a/R/plot_scatter_alignment_exposure.R +++ b/R/plot_scatter_alignment_exposure.R @@ -24,10 +24,10 @@ plot_scatter_alignment_exposure <- function(data, currency) { if (!is.null(group_var)) { if (!inherits(group_var, "character")) { - stop("group_var must be of class character") + cli::cli_abort("{.arg group_var} must be of class {.cls character}") } if (!length(group_var) == 1) { - stop("group_var must be of length 1") + cli::cli_abort("{.arg group_var} must be of length 1") } } else { data <- data %>% diff --git a/R/prep_sankey.R b/R/prep_sankey.R index 12eff5f2..a2b5cd12 100644 --- a/R/prep_sankey.R +++ b/R/prep_sankey.R @@ -23,10 +23,10 @@ prep_sankey <- function(data_alignment, middle_node2 = NULL) { if (!is.null(group_var)) { if (!inherits(group_var, "character")) { - stop("group_var must be of class character") + cli::cli_abort("{.arg group_var} must be of class {.cls character}") } if (!length(group_var) == 1) { - stop("group_var must be of length 1") + cli::cli_abort("{.arg group_var} must be of length 1") } } else { data_alignment <- data_alignment %>% diff --git a/R/prep_scatter.R b/R/prep_scatter.R index 6fb6b992..151b4f6d 100644 --- a/R/prep_scatter.R +++ b/R/prep_scatter.R @@ -17,7 +17,7 @@ #' @return data.frame #' #' @rdname plot_scatter -#' +#' #' @noRd prep_scatter <- function(data_bopo, @@ -32,10 +32,10 @@ prep_scatter <- function(data_bopo, if (!is.null(group_var)) { if (!inherits(group_var, "character")) { - stop("group_var must be of class character") + cli::cli_abort("{.arg group_var} must be of class {.cls character}") } if (!length(group_var) == 1) { - stop("group_var must be of length 1") + cli::cli_abort("{.arg group_var} must be of length 1") } } else { data_bopo <- data_bopo %>% diff --git a/R/prep_scatter_alignment_exposure.R b/R/prep_scatter_alignment_exposure.R index 5c89e6fc..82301e6b 100644 --- a/R/prep_scatter_alignment_exposure.R +++ b/R/prep_scatter_alignment_exposure.R @@ -15,7 +15,7 @@ #' @return data.frame #' #' @rdname plot_scatter_alignment_exposure -#' +#' #' @noRd prep_scatter_alignment_exposure <- function(data, @@ -26,10 +26,10 @@ prep_scatter_alignment_exposure <- function(data, exclude_groups = "benchmark") { if (!is.null(group_var)) { if (!inherits(group_var, "character")) { - stop("group_var must be of class character") + cli::cli_abort("{.arg group_var} must be of class {.cls character}") } if (!length(group_var) == 1) { - stop("group_var must be of length 1") + cli::cli_abort("{.arg group_var} must be of length 1") } } else { data <- data %>% From 981d8d3d400cd74ea97a29063494de9ae73a1b76 Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Tue, 12 Nov 2024 21:41:25 +0100 Subject: [PATCH 4/7] final --- R/plot_sankey.R | 2 +- R/plot_scatter.R | 2 +- R/prep_sankey.R | 8 ++++---- R/prep_scatter.R | 10 +++++----- R/utils.R | 24 ++++++++++++------------ 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/R/plot_sankey.R b/R/plot_sankey.R index 74b9ae76..0d26e90a 100644 --- a/R/plot_sankey.R +++ b/R/plot_sankey.R @@ -167,7 +167,7 @@ check_plot_sankey <- function(data, group_var, capitalise_node_labels) { crucial_names <- c(group_var, "middle_node", "is_aligned", "loan_size_outstanding") - abort_if_missing_names(data, crucial_names) + assert_missing_names(data, crucial_names) if (!is.logical(capitalise_node_labels)) { cli::cli_abort(c( x = "`capitalise_node_labels` must have a {.cls logical} value.", diff --git a/R/plot_scatter.R b/R/plot_scatter.R index 29011da3..1c9efec5 100644 --- a/R/plot_scatter.R +++ b/R/plot_scatter.R @@ -211,7 +211,7 @@ plot_scatter <- function(data, check_plot_scatter <- function(data, alignment_limit, cap_outliers, floor_outliers) { - abort_if_missing_names(data, c( + assert_missing_names(data, c( "name", "buildout", "phaseout", "net" )) diff --git a/R/prep_sankey.R b/R/prep_sankey.R index a2b5cd12..fb89e930 100644 --- a/R/prep_sankey.R +++ b/R/prep_sankey.R @@ -92,7 +92,7 @@ check_prep_sankey <- function(data_alignment, middle_node2) { names_all <- c(group_var, "name_abcd", "sector") names_aggergate <- c("region", "year") - abort_if_missing_names(data_alignment, c(names_all, names_aggergate)) + assert_missing_names(data_alignment, c(names_all, names_aggergate)) if (!(region %in% unique(data_alignment$region))) { cli::cli_abort(c( x = "{.arg region} value not found in {.var data_alignment} dataset", @@ -110,13 +110,13 @@ check_prep_sankey <- function(data_alignment, i = "the value{?s} in {.arg year} {?is/are}: {.val {unique(year)}}" )) } - abort_if_middle_node_column_not_found(data_alignment, middle_node, env = list(data = substitute(data_alignment))) + assert_middle_node_column_not_found(data_alignment, middle_node, env = list(data = substitute(data_alignment))) if (!is.null(middle_node2)) { - abort_if_middle_node_column_not_found(data_alignment, middle_node2, list(data = substitute(data_alignment))) + assert_middle_node_column_not_found(data_alignment, middle_node2, list(data = substitute(data_alignment))) } } -abort_if_middle_node_column_not_found <- function(data, name, env = parent.frame()) { +assert_middle_node_column_not_found <- function(data, name, env = parent.frame()) { .data <- deparse1(substitute(data, env = env)) if (!(name %in% names(data))) { diff --git a/R/prep_scatter.R b/R/prep_scatter.R index 151b4f6d..9b859a8c 100644 --- a/R/prep_scatter.R +++ b/R/prep_scatter.R @@ -96,7 +96,7 @@ check_prep_scatter <- function(data, groups_to_plot, name_col, value_col) { - abort_if_missing_names( + assert_missing_names( data, c( group_var, @@ -108,8 +108,8 @@ check_prep_scatter <- function(data, value_col ) ) - abort_if_unknown_values(sector, data, "sector") - abort_if_unknown_values(region, data, "region") - abort_if_unknown_values(year, data, "year") - abort_if_unknown_values(groups_to_plot, data, group_var) + assert_unknown_values(sector, data, "sector") + assert_unknown_values(region, data, "region") + assert_unknown_values(year, data, "year") + assert_unknown_values(groups_to_plot, data, group_var) } diff --git a/R/utils.R b/R/utils.R index e5df6d91..c874643a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -7,7 +7,7 @@ beautify_scenario_label <- function(label) { #' Check if a named object contains expected names #' -#' Based on fgeo.tool::abort_if_missing_names() +#' Based on fgeo.tool::assert_missing_names() #' #' @param x A named object. #' @param expected_names String; expected names of `x`. @@ -16,12 +16,12 @@ beautify_scenario_label <- function(label) { #' #' @examples #' x <- c(a = 1) -#' abort_if_missing_names(x, "a") -#' try(abort_if_missing_names(x, "bad")) +#' assert_missing_names(x, "a") +#' try(assert_missing_names(x, "bad")) #' #' @noRd -abort_if_missing_names <- function(data, expected_names) { +assert_missing_names <- function(data, expected_names) { if (!rlang::is_named(data)) { cli::cli_abort( message = c(x = "{.arg data} must be named"), @@ -38,10 +38,10 @@ abort_if_missing_names <- function(data, expected_names) { if (!all(unique(expected_names) %in% names(data))) { missing_names <- sort(setdiff(expected_names, names(data))) - rlang::abort( + cli::cli_abort( c( - "`data` must have all the expected names.", - x = glue::glue("Missing names: {toString(missing_names)}.") + x = "{.arg data} must have all the expected names", + i = "Missing names: {.val {missing_names}}" ), class = "missing_names" ) @@ -51,7 +51,7 @@ abort_if_missing_names <- function(data, expected_names) { } -abort_if_unknown_values <- function(value, data, column) { +assert_unknown_values <- function(value, data, column) { if (is.null(value)) { return(invisible(value)) } @@ -62,11 +62,11 @@ abort_if_unknown_values <- function(value, data, column) { valid <- unique(data[[column]]) if (!all(value %in% valid)) { msg <- c( - glue::glue("Each value of `{.value}` must be one of these:\n{toString(valid)}."), - x = glue::glue("You passed: {toString(value)}."), - i = glue::glue("Do you need to see valid values in this dataset?:\n{.data}") + x = "Each value of {.arg {(.value)}} must be one of these: {.val {value}}", + i = "You passed: {.val {value}}", + i = "Do you need to see valid values in this dataset?: {.arg {(.data)}}" ) - rlang::abort(msg, class = "unknown_value") + cli::cli_abort(msg, class = "unknown_value") } invisible(value) From a717cc091930ccc212dc2beb83ecf146e38e89f7 Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Tue, 12 Nov 2024 21:44:30 +0100 Subject: [PATCH 5/7] fix one brittle test --- tests/testthat/test-aggregate_alignment_loanbook_exposure.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/test-aggregate_alignment_loanbook_exposure.R b/tests/testthat/test-aggregate_alignment_loanbook_exposure.R index c4501f6b..8168ef21 100644 --- a/tests/testthat/test-aggregate_alignment_loanbook_exposure.R +++ b/tests/testthat/test-aggregate_alignment_loanbook_exposure.R @@ -155,8 +155,7 @@ test_that("net aggregate results with .by arg as crucial variable returns error" .by = "loan_size_outstanding" ) }, - regexp = "It is not possible to group by the critical columns of the `data` and - `matched` inputs." + regexp = "It is not possible to group by the critical columns of the" ) }) From 523d74b90bd7a8667701e3cda5957a05969664c1 Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Wed, 13 Nov 2024 10:34:17 +0100 Subject: [PATCH 6/7] Update R/aggregate_alignment_loanbook_exposure.R Co-authored-by: Jacob Kastl <60064070+jacobvjk@users.noreply.github.com> --- R/aggregate_alignment_loanbook_exposure.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/aggregate_alignment_loanbook_exposure.R b/R/aggregate_alignment_loanbook_exposure.R index 5d492297..185b1fa7 100644 --- a/R/aggregate_alignment_loanbook_exposure.R +++ b/R/aggregate_alignment_loanbook_exposure.R @@ -44,7 +44,7 @@ aggregate_alignment_loanbook_exposure <- function(data, if (!is.null(.by)) { if (!inherits(.by, "character")) { cli::cli_abort(c( - x = "{.arg .by} must a {.cls character} vector", + x = "{.arg .by} must be a {.cls character} vector", i = "your input is a{.cls {typeof(.by)}}" )) } From 580256f54fb158b20f1f41cbf1a86f00e7e9138c Mon Sep 17 00:00:00 2001 From: CJ Yetman Date: Wed, 13 Nov 2024 10:41:08 +0100 Subject: [PATCH 7/7] fix assert naming --- R/plot_sankey.R | 2 +- R/plot_scatter.R | 2 +- R/prep_sankey.R | 8 ++++---- R/prep_scatter.R | 10 +++++----- R/utils.R | 10 +++++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/R/plot_sankey.R b/R/plot_sankey.R index 0d26e90a..5036039c 100644 --- a/R/plot_sankey.R +++ b/R/plot_sankey.R @@ -167,7 +167,7 @@ check_plot_sankey <- function(data, group_var, capitalise_node_labels) { crucial_names <- c(group_var, "middle_node", "is_aligned", "loan_size_outstanding") - assert_missing_names(data, crucial_names) + assert_no_missing_names(data, crucial_names) if (!is.logical(capitalise_node_labels)) { cli::cli_abort(c( x = "`capitalise_node_labels` must have a {.cls logical} value.", diff --git a/R/plot_scatter.R b/R/plot_scatter.R index 1c9efec5..081a3010 100644 --- a/R/plot_scatter.R +++ b/R/plot_scatter.R @@ -211,7 +211,7 @@ plot_scatter <- function(data, check_plot_scatter <- function(data, alignment_limit, cap_outliers, floor_outliers) { - assert_missing_names(data, c( + assert_no_missing_names(data, c( "name", "buildout", "phaseout", "net" )) diff --git a/R/prep_sankey.R b/R/prep_sankey.R index fb89e930..8dd28ba9 100644 --- a/R/prep_sankey.R +++ b/R/prep_sankey.R @@ -92,7 +92,7 @@ check_prep_sankey <- function(data_alignment, middle_node2) { names_all <- c(group_var, "name_abcd", "sector") names_aggergate <- c("region", "year") - assert_missing_names(data_alignment, c(names_all, names_aggergate)) + assert_no_missing_names(data_alignment, c(names_all, names_aggergate)) if (!(region %in% unique(data_alignment$region))) { cli::cli_abort(c( x = "{.arg region} value not found in {.var data_alignment} dataset", @@ -110,13 +110,13 @@ check_prep_sankey <- function(data_alignment, i = "the value{?s} in {.arg year} {?is/are}: {.val {unique(year)}}" )) } - assert_middle_node_column_not_found(data_alignment, middle_node, env = list(data = substitute(data_alignment))) + assert_middle_node_column_exists(data_alignment, middle_node, env = list(data = substitute(data_alignment))) if (!is.null(middle_node2)) { - assert_middle_node_column_not_found(data_alignment, middle_node2, list(data = substitute(data_alignment))) + assert_middle_node_column_exists(data_alignment, middle_node2, list(data = substitute(data_alignment))) } } -assert_middle_node_column_not_found <- function(data, name, env = parent.frame()) { +assert_middle_node_column_exists <- function(data, name, env = parent.frame()) { .data <- deparse1(substitute(data, env = env)) if (!(name %in% names(data))) { diff --git a/R/prep_scatter.R b/R/prep_scatter.R index 9b859a8c..6829fde7 100644 --- a/R/prep_scatter.R +++ b/R/prep_scatter.R @@ -96,7 +96,7 @@ check_prep_scatter <- function(data, groups_to_plot, name_col, value_col) { - assert_missing_names( + assert_no_missing_names( data, c( group_var, @@ -108,8 +108,8 @@ check_prep_scatter <- function(data, value_col ) ) - assert_unknown_values(sector, data, "sector") - assert_unknown_values(region, data, "region") - assert_unknown_values(year, data, "year") - assert_unknown_values(groups_to_plot, data, group_var) + assert_no_unknown_values(sector, data, "sector") + assert_no_unknown_values(region, data, "region") + assert_no_unknown_values(year, data, "year") + assert_no_unknown_values(groups_to_plot, data, group_var) } diff --git a/R/utils.R b/R/utils.R index c874643a..580ddfc7 100644 --- a/R/utils.R +++ b/R/utils.R @@ -7,7 +7,7 @@ beautify_scenario_label <- function(label) { #' Check if a named object contains expected names #' -#' Based on fgeo.tool::assert_missing_names() +#' Based on fgeo.tool::abort_if_missing_names() #' #' @param x A named object. #' @param expected_names String; expected names of `x`. @@ -16,12 +16,12 @@ beautify_scenario_label <- function(label) { #' #' @examples #' x <- c(a = 1) -#' assert_missing_names(x, "a") -#' try(assert_missing_names(x, "bad")) +#' assert_no_missing_names(x, "a") +#' try(assert_no_missing_names(x, "bad")) #' #' @noRd -assert_missing_names <- function(data, expected_names) { +assert_no_missing_names <- function(data, expected_names) { if (!rlang::is_named(data)) { cli::cli_abort( message = c(x = "{.arg data} must be named"), @@ -51,7 +51,7 @@ assert_missing_names <- function(data, expected_names) { } -assert_unknown_values <- function(value, data, column) { +assert_no_unknown_values <- function(value, data, column) { if (is.null(value)) { return(invisible(value)) }