|
48 | 48 | #' calc_crwidth(plantation) |
49 | 49 | #' @export |
50 | 50 | 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 | + } |
53 | 56 |
|
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 | + } |
56 | 62 |
|
57 | 63 | 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 | + } |
75 | 105 |
|
76 | 106 | if (is.null(digits)) |
77 | 107 | digits <- 1 |
|
0 commit comments