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

convert all stop() and rlang::abort() commands to cli::cli_abort() #232

Merged
merged 8 commits into from
Nov 13, 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
11 changes: 7 additions & 4 deletions R/aggregate_alignment_loanbook_exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 be a {.cls character} vector",
i = "your input is a{.cls {typeof(.by)}}"
))
}
group_vars <- c(.by, group_vars)
}
Expand Down Expand Up @@ -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!"
)
}
}
Expand Down
116 changes: 61 additions & 55 deletions R/calculate_company_alignment_metric.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
)
)
}
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)}"
)
)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)}"
)
)
}
Expand All @@ -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}"
)
)
}
Expand Down
26 changes: 11 additions & 15 deletions R/plot_sankey.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>%
Expand Down Expand Up @@ -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."
)
}
}
Expand All @@ -169,13 +167,11 @@ 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_no_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}}."
))
}
}
17 changes: 13 additions & 4 deletions R/plot_scatter.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,32 @@ plot_scatter <- function(data,


check_plot_scatter <- function(data, alignment_limit, cap_outliers, floor_outliers) {
abort_if_missing_names(data, c(
assert_no_missing_names(data, c(
"name", "buildout",
"phaseout", "net"
))
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}}"
))
}
}
}
4 changes: 2 additions & 2 deletions R/plot_scatter_alignment_exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>%
Expand Down
Loading