Skip to content

Commit 7e48f9b

Browse files
committed
use cli for error conditions
1 parent 1b13595 commit 7e48f9b

2 files changed

Lines changed: 82 additions & 31 deletions

File tree

R/calc_crown_overlay.R

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,41 @@
4141
#' plot_crowns(trees, subplot = 1, main = "plantation subplot 1")
4242
#' @export
4343
calc_crown_overlay <- function(tree_list, sample_radius, digits = 1) {
44-
if (missing(tree_list) || is.null(tree_list))
45-
stop("'tree_list' is required", call. = FALSE)
44+
if (missing(tree_list) || is.null(tree_list)) {
45+
stop(cli::format_error(c(
46+
"{.arg tree_list} is required",
47+
"x" = "A required argument is missing or NULL")))
48+
}
4649

47-
if (!is.data.frame(tree_list))
48-
stop("'tree_list' must be a data frame", call. = FALSE)
50+
if (!is.data.frame(tree_list)) {
51+
stop(cli::format_error(c(
52+
"{.arg tree_list} must be a {.cls data.frame}",
53+
"x" = "Invalid input type: {.cls {class(tree_list)}}")))
54+
}
4955

50-
if (any(tree_list$AZIMUTH < 0) || any(tree_list$AZIMUTH > 360))
51-
stop("'tree_list$AZIMUTH' contains values out of range", call. = FALSE)
56+
if (any(is.na(tree_list$AZIMUTH))) {
57+
stop(cli::format_error(c(
58+
"{.fld tree_list$AZIMUTH} has missing values",
59+
"x" = "{.fld tree_list$AZIMUTH} cannot contain {.val {NA}} values")))
60+
}
5261

53-
if (missing(sample_radius) || is.null(sample_radius))
54-
stop("'sample_radius' is required", call. = FALSE)
62+
if (any(tree_list$AZIMUTH < 0) || any(tree_list$AZIMUTH > 360)) {
63+
stop(cli::format_error(c(
64+
"{.fld tree_list$AZIMUTH} contains out-of-range values",
65+
"x" = "azimuth values must be in the range 0:360")))
66+
}
5567

56-
if (!(is.numeric(sample_radius) && length(sample_radius) == 1))
57-
stop("'sample_radius' must be a single numeric value", call. = FALSE)
68+
if (missing(sample_radius) || is.null(sample_radius)) {
69+
stop(cli::format_error(c(
70+
"{.arg sample_radius} is required",
71+
"x" = "A required argument is missing or NULL")))
72+
}
73+
74+
if (!(is.numeric(sample_radius) && length(sample_radius) == 1)) {
75+
stop(cli::format_error(c(
76+
"{.arg sample_radius} must be a single {.cls numeric} value",
77+
"x" = "Invalid input: length-{length(sample_radius)} {.cls {class(sample_radius)}}")))
78+
}
5879

5980
if (is.null(digits))
6081
digits <- 1

R/calc_crwidth.R

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,60 @@
4848
#' calc_crwidth(plantation)
4949
#' @export
5050
calc_crwidth <- function(tree_table, digits = 1) {
51-
if (missing(tree_table) || is.null(tree_table))
52-
stop("'tree_table' is required", call. = FALSE)
51+
if (missing(tree_table) || is.null(tree_table)) {
52+
stop(cli::format_error(c(
53+
"{.arg tree_table} is required",
54+
"x" = "A required argument is missing or NULL")))
55+
}
5356

54-
if (!is.data.frame(tree_table))
55-
stop("'tree_table' must be a data frame", call. = FALSE)
57+
if (!is.data.frame(tree_table)) {
58+
stop(cli::format_error(c(
59+
"{.arg tree_table} must be a {.cls data.frame}",
60+
"x" = "Invalid input type: {.cls {class(tree_table)}}")))
61+
}
5662

5763
required_cols <- c("SPCD", "STATUSCD", "DIA")
58-
if (!all(required_cols %in% colnames(tree_table)))
59-
stop("'tree_table' is missing required columns", call. = FALSE)
60-
61-
if (!is.numeric(tree_table$SPCD))
62-
stop("'SPCD' must be numeric or integer", call. = FALSE)
63-
if (any(is.na(tree_table$SPCD)))
64-
stop("'SPCD' cannot have missing values", call. = FALSE)
65-
66-
if (!is.numeric(tree_table$STATUSCD))
67-
stop("'STATUSCD' must be numeric or integer", call. = FALSE)
68-
if (any(is.na(tree_table$STATUSCD)))
69-
stop("'STATUSCD' cannot have missing values", call. = FALSE)
70-
71-
if (!is.numeric(tree_table$DIA))
72-
stop("'DIA' must be numeric", call. = FALSE)
73-
if (any(is.na(tree_table$DIA[tree_table$STATUSCD == 1])))
74-
stop("'DIA' has missing values for live trees", call. = FALSE)
64+
if (!all(required_cols %in% colnames(tree_table))) {
65+
stop(cli::format_error(c(
66+
"{.arg tree_table} is missing one or more required columns",
67+
"x" = "Missing column(s): {.fld {setdiff(required_cols, colnames(tree_table))}}")))
68+
}
69+
70+
if (!is.numeric(tree_table$SPCD)) {
71+
stop(cli::format_error(c(
72+
"{.fld tree_table$SPCD} must be a {.cls numeric} type",
73+
"x" = "Invalid field type: {.cls {class(tree_table$SPCD)}}")))
74+
}
75+
76+
if (any(is.na(tree_table$SPCD))) {
77+
stop(cli::format_error(c(
78+
"{.fld tree_table$SPCD} has missing values",
79+
"x" = "{.fld tree_table$SPCD} cannot contain {.val {NA}}")))
80+
}
81+
82+
if (!is.numeric(tree_table$STATUSCD)) {
83+
stop(cli::format_error(c(
84+
"{.fld tree_table$STATUSCD} must be a {.cls numeric} type",
85+
"x" = "Invalid field type: {.cls {class(tree_table$STATUSCD)}}")))
86+
}
87+
88+
if (any(is.na(tree_table$STATUSCD))) {
89+
stop(cli::format_error(c(
90+
"{.fld tree_table$STATUSCD} has missing values",
91+
"x" = "{.fld tree_table$STATUSCD} cannot contain {.val {NA}}")))
92+
}
93+
94+
if (!is.numeric(tree_table$DIA)) {
95+
stop(cli::format_error(c(
96+
"{.fld tree_table$DIA} must be {.cls numeric}",
97+
"x" = "Invalid field type: {.cls {class(tree_table$DIA)}}")))
98+
}
99+
100+
if (any(is.na(tree_table$DIA[tree_table$STATUSCD == 1]))) {
101+
stop(cli::format_error(c(
102+
"{.fld tree_table$DIA} has missing values for live trees",
103+
"x" = "{.fld tree_table$DIA} cannot have {.val {NA}} for live trees")))
104+
}
75105

76106
if (is.null(digits))
77107
digits <- 1

0 commit comments

Comments
 (0)