Skip to content

Commit b75fd61

Browse files
committed
feat: use cli for errors and messaging
1 parent 70e55ac commit b75fd61

File tree

17 files changed

+113
-116
lines changed

17 files changed

+113
-116
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Depends:
3939
R (>= 3.4.0),
4040
fabletools (>= 0.3.0)
4141
Imports:
42+
cli,
4243
Rcpp (>= 0.11.0),
4344
rlang (>= 0.4.6),
4445
stats,

R/00_specials.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ model_xreg <- function(...) {
1313
}
1414

1515
no_xreg <- function(...) {
16-
abort("Exogenous regressors are not supported for this model type.")
16+
cli::cli_abort("Exogenous regressors are not supported for this model type.")
1717
}
1818

1919
trend <- function(x, knots = NULL, origin = NULL) {
@@ -91,10 +91,10 @@ fourier.tbl_ts <- function(x, period, K, origin = NULL) {
9191

9292
fourier.numeric <- function(x, period, K, origin = NULL) {
9393
if (length(period) != length(K)) {
94-
abort("Number of periods does not match number of orders")
94+
cli::cli_abort("Number of periods does not match number of orders")
9595
}
9696
if (any(2 * K > period)) {
97-
abort("K must be not be greater than period/2")
97+
cli::cli_abort("{.arg K} must be not be greater than period/2")
9898
}
9999

100100
fourier_exprs <- map2(

R/VARIMA.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ train_varima <- function(.data, specials, identification = NULL, ...) {
33
y <- invoke(cbind, lapply(unclass(.data)[measured_vars(.data)], as.double))
44

55
if(any(colnames(specials$xreg[[1]]) != "(Intercept)")) {
6-
stop("Exogenous regressors for VARIMA are not yet supported.")
6+
cli::cli_abort("Exogenous regressors for VARIMA are not yet supported.")
77
}
88

99
p <- specials$pdq[[1]]$p
@@ -20,7 +20,7 @@ train_varima <- function(.data, specials, identification = NULL, ...) {
2020
}
2121
}
2222

23-
require_package("MTS")
23+
check_installed("MTS")
2424
utils::capture.output(
2525
fit <- if (identification == "kronecker_indices") {
2626
MTS::Kronfit(
@@ -39,7 +39,7 @@ train_varima <- function(.data, specials, identification = NULL, ...) {
3939
)
4040
} else {
4141
if(length(p) != 1 || length(q) != 1) {
42-
stop("Model selection is not yet supported, please specify `p` and `q` exactly.")
42+
cli::cli_abort("Model selection is not yet supported, please specify {.arg p} and {.arg q} exactly.")
4343
}
4444
MTS::VARMA(
4545
yd,
@@ -72,7 +72,7 @@ specials_varima <- new_specials(
7272
as.list(environment())
7373
},
7474
PDQ = function(P, D, Q, period = NULL) {
75-
stop("Seasonal VARIMA models are not yet supported.")
75+
cli::cli_abort("Seasonal VARIMA models are not yet supported.")
7676
},
7777
common_xregs,
7878
xreg = special_xreg(default_intercept = TRUE),
@@ -580,4 +580,4 @@ IRF.VARIMA <- function(x, new_data, specials, impulse = NULL, orthogonal = FALSE
580580
irf[colnames(x$data)] <- split(irf$.sim, col(irf$.sim))
581581
irf$.innov <- irf$.sim <- NULL
582582
irf
583-
}
583+
}

R/ar.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ AR <- function(formula, ic = c("aicc", "aic", "bic"), ...) {
7272
specials_ar <- new_specials(
7373
order = function(p = 0:15, fixed = list()) {
7474
if (any(p < 0)) {
75-
warn("The AR order must be non-negative. Only non-negative orders will be considered.")
75+
cli::cli_warn("The AR order must be non-negative. Only non-negative orders will be considered.")
7676
p <- p[p >= 0]
7777
}
7878
list(p = p, fixed = fixed)
@@ -158,9 +158,9 @@ estimate_ar <- function(x, p, xreg, constant, fixed) {
158158
XX <- t(X_est) %*% X_est
159159
rank <- qr(XX)$rank
160160
if (rank != nrow(XX)) {
161-
warning(paste("model order: ", p, "singularities in the computation of the projection matrix",
162-
"results are only valid up to model order",
163-
p - 1L), domain = NA)
161+
cli::cli_warn(
162+
"model order: {p} singularities in the computation of the projection matrix results are only valid up to model order {p - 1L}"
163+
)
164164
return(NULL)
165165
}
166166
P <- if (ncol(XX) > 0)

R/arima.R

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ train_arima <- function(.data, specials,
88
unitroot_spec = unitroot_options(), trace = FALSE,
99
fixed = NULL, method = NULL, ...) {
1010
if (length(measured_vars(.data)) > 1) {
11-
abort("Only univariate responses are supported by ARIMA.")
11+
cli::cli_abort("Only univariate responses are supported by ARIMA.")
1212
}
1313

1414
# Get args
1515
if(length(specials$pdq) > 1 || length(specials$PDQ) > 1){
16-
warn("Only one special for `pdq()` and `PDQ()` is allowed, defaulting to the first usage")
16+
cli::cli_warn("Only one special for {.fn pdq} and {.fn PDQ} is allowed, defaulting to the first usage")
1717
}
1818
pdq <- specials$pdq[[1]]
1919
PDQ <- specials$PDQ[[1]]
@@ -24,7 +24,7 @@ train_arima <- function(.data, specials,
2424
y <- x <- ts(unclass(.data)[[measured_vars(.data)]], frequency = period)
2525

2626
if (all(is.na(y))) {
27-
abort("All observations are missing, a model cannot be estimated without data.")
27+
cli::cli_abort("All observations are missing, a model cannot be estimated without data.")
2828
}
2929

3030
# Get xreg
@@ -47,7 +47,7 @@ train_arima <- function(.data, specials,
4747

4848
# Remove deficient regressors
4949
if(!is_empty(bad_regressors)){
50-
warn(sprintf(
50+
cli::cli_warn(sprintf(
5151
"Provided exogenous regressors are rank deficient, removing regressors: %s",
5252
paste("`", colnames(xreg)[bad_regressors], "`", sep = "", collapse = ", ")
5353
))
@@ -72,8 +72,8 @@ train_arima <- function(.data, specials,
7272
if (NROW(model_opts) > 1) {
7373
model_opts <- filter(model_opts, !!enexpr(order_constraint))
7474
if (NROW(model_opts) == 0) {
75-
if (mostly_specified) warn(mostly_specified_msg)
76-
abort("There are no ARIMA models to choose from after imposing the `order_constraint`, please consider allowing more models.")
75+
if (mostly_specified) cli::cli_warn(mostly_specified_msg)
76+
cli::cli_abort("There are no ARIMA models to choose from after imposing the {.arg order_constraint}, please consider allowing more models.")
7777
}
7878
wrap_arima <- possibly(quietly(stats::arima), NULL)
7979
}
@@ -90,7 +90,7 @@ train_arima <- function(.data, specials,
9090

9191
# Choose seasonal differencing
9292
if (length(seas_D <- unique(model_opts$D)) > 1) {
93-
require_package("feasts")
93+
rlang::check_installed("feasts")
9494
# Valid xregs
9595

9696
if (!is.null(xreg)) {
@@ -111,7 +111,7 @@ train_arima <- function(.data, specials,
111111
x <- diff(x, lag = period, differences = seas_D)
112112
diff_xreg <- diff(xreg, lag = period, differences = seas_D)
113113
if (length(seas_d <- unique(model_opts$d)) > 1) {
114-
require_package("feasts")
114+
check_installed("feasts")
115115

116116
# Valid xregs
117117
if (!is.null(xreg)) {
@@ -132,12 +132,12 @@ train_arima <- function(.data, specials,
132132
}
133133

134134
# Check number of differences selected
135-
if (length(seas_D) != 1) abort("Could not find appropriate number of seasonal differences.")
136-
if (length(seas_d) != 1) abort("Could not find appropriate number of non-seasonal differences.")
135+
if (length(seas_D) != 1) cli::cli_abort("Could not find appropriate number of seasonal differences.")
136+
if (length(seas_d) != 1) cli::cli_abort("Could not find appropriate number of non-seasonal differences.")
137137
if (seas_D >= 2) {
138-
warn("Having more than one seasonal difference is not recommended. Please consider using only one seasonal difference.")
138+
cli::cli_warn("Having more than one seasonal difference is not recommended. Please consider using only one seasonal difference.")
139139
} else if (seas_D + seas_d > 2) {
140-
warn("Having 3 or more differencing operations is not recommended. Please consider reducing the total number of differences.")
140+
cli::cli_warn("Having 3 or more differencing operations is not recommended. Please consider reducing the total number of differences.")
141141
}
142142

143143
# Find best model
@@ -236,7 +236,7 @@ train_arima <- function(.data, specials,
236236
method <- "CSS-ML"
237237
}
238238
} else {
239-
if(isTRUE(approximation)) warn("Estimating ARIMA models with approximation is not supported when `method` is specified.")
239+
if(isTRUE(approximation)) cli::cli_warn("Estimating ARIMA models with approximation is not supported when {.arg method} is specified.")
240240
approximation <- FALSE
241241
}
242242

@@ -252,7 +252,7 @@ train_arima <- function(.data, specials,
252252
}
253253

254254
if (any((model_opts$d + model_opts$D > 1) & model_opts$constant)) {
255-
warn("Model specification induces a quadratic or higher order polynomial trend.
255+
cli::cli_warn("Model specification induces a quadratic or higher order polynomial trend.
256256
This is generally discouraged, consider removing the constant or reducing the number of differences.")
257257
}
258258
constant <- unique(model_opts$constant)
@@ -337,8 +337,12 @@ This is generally discouraged, consider removing the constant or reducing the nu
337337
}
338338

339339
if (is.null(best)) {
340-
if (mostly_specified) warn(mostly_specified_msg)
341-
abort("Could not find an appropriate ARIMA model.\nThis is likely because automatic selection does not select models with characteristic roots that may be numerically unstable.\nFor more details, refer to https://otexts.com/fpp3/arima-r.html#plotting-the-characteristic-roots")
340+
if (mostly_specified) cli::cli_warn(mostly_specified_msg)
341+
cli::cli_abort(c(
342+
"Could not find an appropriate ARIMA model.",
343+
"This is likely because automatic selection does not select models with characteristic roots that may be numerically unstable.",
344+
"For more details, refer to {.url https://otexts.com/fpp3/arima-r.html#plotting-the-characteristic-roots}"
345+
))
342346
}
343347

344348
# Compute ARMA roots
@@ -420,7 +424,7 @@ specials_arima <- new_specials(
420424
p_init <- p[which.min(abs(p - p_init))]
421425
q_init <- q[which.min(abs(q - q_init))]
422426
if(!all(grepl("^(ma|ar)\\d+", names(fixed)))){
423-
abort("The 'fixed' coefficients for pdq() must begin with ar or ma, followed by a lag number.")
427+
cli::cli_abort("The {.arg fixed} coefficients for {.fn pdq} must begin with ar or ma, followed by a lag number.")
424428
}
425429
as.list(environment())
426430
},
@@ -429,7 +433,7 @@ specials_arima <- new_specials(
429433
fixed = list()) {
430434
period <- get_frequencies(period, self$data, .auto = "smallest")
431435
if (period < 1) {
432-
abort("The seasonal period must be greater than or equal to 1.")
436+
cli::cli_abort("The seasonal period must be greater than or equal to 1.")
433437
} else if (period == 1) {
434438
# Not seasonal
435439
P <- 0
@@ -440,13 +444,13 @@ specials_arima <- new_specials(
440444
P <- P[P <= floor(NROW(self$data) / 3 / period)]
441445
Q <- Q[Q <= floor(NROW(self$data) / 3 / period)]
442446
if(length(P) == 0 || length(Q) == 0) {
443-
abort("Not enough data to estimate a model with those options of P and Q. Consider allowing smaller values of P and Q to be selected.")
447+
cli::cli_abort("Not enough data to estimate a model with those options of P and Q. Consider allowing smaller values of P and Q to be selected.")
444448
}
445449
}
446450
P_init <- P[which.min(abs(P - P_init))]
447451
Q_init <- Q[which.min(abs(Q - Q_init))]
448452
if(!all(grepl("^(sma|sar)\\d+", names(fixed)))){
449-
abort("The 'fixed' coefficients for PDQ() must begin with sar or sma, followed by a lag number.")
453+
cli::cli_abort("The {.arg fixed} coefficients for {.fn PDQ} must begin with sar or sma, followed by a lag number.")
450454
}
451455
as.list(environment())
452456
},
@@ -716,7 +720,7 @@ residuals.ARIMA <- function(object, type = c("innovation", "regression"), ...) {
716720
object$est[[".regression_resid"]]
717721
}
718722
else {
719-
abort(sprintf('Residuals of `type = "%s"` are not supported by ARIMA models', type))
723+
cli::cli_abort("Residuals of {.code type = {.val type}} are not supported by ARIMA models")
720724
}
721725
}
722726

R/checks.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
check_gaps <- function(x) {
22
if (any(tsibble::has_gaps(x)[[".gaps"]])) {
3-
abort(sprintf("%s contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using `tsibble::fill_gaps()` if required.", deparse(substitute(x))))
3+
cli::cli_abort(sprintf("%s contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using {.fn tsibble::fill_gaps} if required.", deparse(substitute(x))))
44
}
55
}
66

77
check_regular <- function(x) {
88
if (!is_regular(x)) {
9-
abort(sprintf("%s is an irregular time series, which this model does not support. You should consider if your data can be made regular, and use `tsibble::update_tsibble(%s, regular = TRUE)` if appropriate.", deparse(substitute(x)), deparse(substitute(x))))
9+
cli::cli_abort(sprintf("%s is an irregular time series, which this model does not support. You should consider if your data can be made regular, and use {.fn tsibble::update_tsibble(%s, regular = TRUE)} if appropriate.", deparse(substitute(x)), deparse(substitute(x))))
1010
}
1111
}
1212

1313
check_ordered <- function(x) {
1414
if (!is_ordered(x)) {
15-
abort(sprintf(
16-
"%s is an unordered time series. To use this model, you first must sort the data in time order using `dplyr::arrange(%s, %s)`",
15+
cli::cli_abort(sprintf(
16+
"%s is an unordered time series. To use this model, you first must sort the data in time order using {.fn dplyr::arrange(%s, %s)}",
1717
deparse(substitute(x)), paste(c(deparse(substitute(x)), key_vars(x)), collapse = ", "), index_var(x)
1818
))
1919
}
@@ -24,6 +24,6 @@ all_tsbl_checks <- function(.data) {
2424
check_regular(.data)
2525
check_ordered(.data)
2626
if (NROW(.data) == 0) {
27-
abort("There is no data to model. Please provide a dataset with at least one observation.")
27+
cli::cli_abort("There is no data to model. Please provide a dataset with at least one observation.")
2828
}
2929
}

R/croston.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ CROSTON <- function(
9999
specials_croston <- new_specials(
100100
demand = function(initial = NULL, param = NULL, param_range = c(0, 1)) {
101101
if (!is.null(initial) && initial < 0) {
102-
abort("The initial demand for Croston's method must be non-negative")
102+
cli::cli_abort("The initial demand for Croston's method must be non-negative")
103103
}
104104
if (param_range[1] > param_range[2]) {
105-
rlang::abort("Lower param limits must be less than upper limits")
105+
cli::cli_abort("Lower param limits must be less than upper limits")
106106
}
107107

108108
as.list(environment())
@@ -111,11 +111,11 @@ specials_croston <- new_specials(
111111
method <- match.arg(method)
112112

113113
if (!is.null(initial) && initial < 1) {
114-
abort("The initial interval for Croston's method must be greater than (or equal to) 1.")
114+
cli::cli_abort("The initial interval for Croston's method must be greater than (or equal to) 1.")
115115
}
116116

117117
if (param_range[1] > param_range[2]) {
118-
rlang::abort("Lower param limits must be less than upper limits")
118+
cli::cli_abort("Lower param limits must be less than upper limits")
119119
}
120120

121121
as.list(environment())
@@ -125,21 +125,21 @@ specials_croston <- new_specials(
125125

126126
train_croston <- function(.data, specials, opt_crit = "mse", type = "croston", ...) {
127127
if (length(measured_vars(.data)) > 1) {
128-
abort("Only univariate responses are supported by Croston's method.")
128+
cli::cli_abort("Only univariate responses are supported by Croston's method.")
129129
}
130130

131131
# Get response
132132
y <- unclass(.data)[[measured_vars(.data)]]
133133

134134
# Check data
135135
if (any(y < 0)) {
136-
abort("All observations must be non-negative for Croston's method.")
136+
cli::cli_abort("All observations must be non-negative for Croston's method.")
137137
}
138138

139139
non_zero <- which(y != 0)
140140

141141
if (length(non_zero) < 2) {
142-
abort("At least two non-zero values are required to use Croston's method.")
142+
cli::cli_abort("At least two non-zero values are required to use Croston's method.")
143143
}
144144

145145
# Get specials

0 commit comments

Comments
 (0)