Skip to content

Commit f185dce

Browse files
authored
transition to cli from rlang (#1161)
1 parent 149f601 commit f185dce

File tree

5 files changed

+51
-32
lines changed

5 files changed

+51
-32
lines changed

R/bart.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ update.bart <-
142142
#' @param std_err Attach column for standard error of prediction or not.
143143
bartMachine_interval_calc <- function(new_data, obj, ci = TRUE, level = 0.95) {
144144
if (obj$spec$mode == "classification") {
145-
rlang::abort("In bartMachine: Prediction intervals are not possible for classification")
145+
cli::cli_abort(
146+
"Prediction intervals are not possible for classification"
147+
)
146148
}
147149
get_std_err <- obj$spec$method$pred$pred_int$extras$std_error
148150

R/boost_tree.R

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,9 @@ translate.boost_tree <- function(x, engine = x$engine, ...) {
134134

135135
if (engine == "spark") {
136136
if (x$mode == "unknown") {
137-
rlang::abort(
138-
glue::glue(
139-
"For spark boosted trees models, the mode cannot be 'unknown' ",
140-
"if the specification is to be translated."
141-
)
137+
cli::cli_abort(
138+
"For spark boosted tree models, the mode cannot be {.val unknown}
139+
if the specification is to be translated."
142140
)
143141
} else {
144142
arg_vals$type <- x$mode
@@ -172,7 +170,7 @@ check_args.boost_tree <- function(object, call = rlang::caller_env()) {
172170
check_number_decimal(args$sample_size, min = 0, max = 1, allow_null = TRUE, call = call, arg = "sample_size")
173171
check_number_whole(args$tree_depth, min = 0, allow_null = TRUE, call = call, arg = "tree_depth")
174172
check_number_whole(args$min_n, min = 0, allow_null = TRUE, call = call, arg = "min_n")
175-
173+
176174
invisible(object)
177175
}
178176

@@ -229,15 +227,15 @@ xgb_train <- function(
229227
num_class <- length(levels(y))
230228

231229
if (!is.numeric(validation) || validation < 0 || validation >= 1) {
232-
rlang::abort("`validation` should be on [0, 1).")
230+
cli::cli_abort("{.arg validation} should be on [0, 1).")
233231
}
234232

235233
if (!is.null(early_stop)) {
236234
if (early_stop <= 1) {
237-
rlang::abort(paste0("`early_stop` should be on [2, ", nrounds, ")."))
235+
cli::cli_abort("{.arg early_stop} should be on [2, {nrounds}).")
238236
} else if (early_stop >= nrounds) {
239237
early_stop <- nrounds - 1
240-
rlang::warn(paste0("`early_stop` was reduced to ", early_stop, "."))
238+
cli::cli_warn("{.arg early_stop} was reduced to {early_stop}.")
241239
}
242240
}
243241

@@ -252,7 +250,7 @@ xgb_train <- function(
252250

253251

254252
if (!is.numeric(subsample) || subsample < 0 || subsample > 1) {
255-
rlang::abort("`subsample` should be on [0, 1].")
253+
cli::cli_abort("{.arg subsample} should be on [0, 1].")
256254
}
257255

258256
# initialize
@@ -268,9 +266,13 @@ xgb_train <- function(
268266
}
269267

270268
if (min_child_weight > n) {
271-
msg <- paste0(min_child_weight, " samples were requested but there were ",
272-
n, " rows in the data. ", n, " will be used.")
273-
rlang::warn(msg)
269+
cli::cli_warn(
270+
c(
271+
"!" = "{min_child_weight} samples were requested but there were {n} rows
272+
in the data.",
273+
"i" = "{n} will be used."
274+
)
275+
)
274276
min_child_weight <- min(min_child_weight, n)
275277
}
276278

@@ -369,14 +371,16 @@ recalc_param <- function(x, counts, denom) {
369371
x
370372
}
371373

372-
maybe_proportion <- function(x, nm) {
374+
maybe_proportion <- function(x, nm, call = rlang::caller_env()) {
373375
if (x < 1) {
374-
msg <- paste0(
375-
"The option `counts = TRUE` was used but parameter `", nm,
376-
"` was given as ", signif(x, 3), ". Please use a value >= 1 or use ",
377-
"`counts = FALSE`."
376+
cli::cli_abort(
377+
c(
378+
"The option `counts = TRUE` was used but {.arg {nm}} was given
379+
as {signif(x, 3)}.",
380+
"i" = "Please use a value >= 1 or use {.code counts = FALSE}."
381+
),
382+
call = call
378383
)
379-
rlang::abort(msg)
380384
}
381385
}
382386

@@ -418,7 +422,9 @@ as_xgb_data <- function(x, y, validation = 0, weights = NULL, event_level = "fir
418422
y <- as.numeric(y) - 1
419423
}
420424
} else {
421-
if (event_level == "second") rlang::warn("`event_level` can only be set for binary variables.")
425+
if (event_level == "second") {
426+
cli::cli_warn("{.arg event_level} can only be set for binary outcomes.")
427+
}
422428
y <- as.numeric(y) - 1
423429
}
424430
}
@@ -573,15 +579,19 @@ C5.0_train <-
573579

574580
n <- nrow(x)
575581
if (n == 0) {
576-
rlang::abort("There are zero rows in the predictor set.")
582+
cli::cli_abort("There are zero rows in the predictor set.")
577583
}
578584

579585

580586
ctrl <- call2("C5.0Control", .ns = "C50")
581587
if (minCases > n) {
582-
msg <- paste0(minCases, " samples were requested but there were ",
583-
n, " rows in the data. ", n, " will be used.")
584-
rlang::warn(msg)
588+
589+
cli::cli_warn(
590+
c(
591+
"!" = "{minCases} samples were requested but there were {n} rows in the data.",
592+
"i" = "{n} will be used."
593+
)
594+
)
585595
minCases <- n
586596
}
587597
ctrl$minCases <- minCases

R/decision_tree.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ translate.decision_tree <- function(x, engine = x$engine, ...) {
9797

9898
if (x$engine == "spark") {
9999
if (x$mode == "unknown") {
100-
rlang::abort(
101-
glue::glue(
102-
"For spark decision tree models, the mode cannot be 'unknown' ",
103-
"if the specification is to be translated."
104-
)
100+
cli::cli_abort(
101+
"For spark decision tree models, the mode cannot be {.val unknown}
102+
if the specification is to be translated."
105103
)
106104
}
107105
}

tests/testthat/_snaps/boost_tree_xgboost.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
Error in `multi_predict()`:
77
! Please use `new_data` instead of `newdata`.
88

9+
# xgboost data conversion
10+
11+
Code
12+
from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y, event_level = "second")
13+
Condition
14+
Warning:
15+
`event_level` can only be set for binary outcomes.
16+
917
# interface to param arguments
1018

1119
! Please supply elements of the `params` list argument as main arguments to `set_engine()` rather than as part of `params`.

tests/testthat/test-boost_tree_xgboost.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,9 @@ test_that('xgboost data conversion', {
416416
expect_equal(xgboost::getinfo(from_df$data, name = "label")[1:5], rep(1, 5))
417417

418418
mtcars_y <- factor(mtcars$mpg < 15, levels = c(TRUE, FALSE, "na"), labels = c("low", "high", "missing"))
419-
expect_warning(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y, event_level = "second"),
420-
regexp = "`event_level` can only be set for binary variables.")
419+
expect_snapshot(
420+
from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y, event_level = "second")
421+
)
421422

422423
# case weights added
423424
expect_error(wted <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, weights = wts), regexp = NA)

0 commit comments

Comments
 (0)