Skip to content

Replace base R message/warning/stop with {cli} #762

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

Merged
merged 16 commits into from
Sep 1, 2024
Merged
Prev Previous commit
Next Next commit
Replace base messages with cli everywhere
  • Loading branch information
jamesmbaazam committed Aug 29, 2024
commit 18a6e99e5b76d87002cb0461a2ceabf518781498
46 changes: 29 additions & 17 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ check_reports_valid <- function(data,
#' @param dist A `dist_spec` object.`
#' @importFrom checkmate assert_class
#' @importFrom rlang arg_match
#' @importFrom cli cli_abort col_blue
#' @return Called for its side effects.
#' @keywords internal
check_stan_delay <- function(dist) {
Expand All @@ -73,9 +74,12 @@ check_stan_delay <- function(dist) {
if (
!all(distributions %in% c("lognormal", "gamma", "fixed", "nonparametric"))
) {
stop(
"Distributions passed to the model need to be lognormal, gamma, fixed ",
"or nonparametric."
cli_abort(
c(
"!" = "Distributions passed to the model need to be
{col_blue(\"lognormal\")}, {col_blue(\"gamma\")},
{col_blue(\"fixed\")}, or {col_blue(\"nonparametric\")}."
)
)
}
# Check that `dist` has parameters that are either numeric or normal
Expand All @@ -91,10 +95,13 @@ check_stan_delay <- function(dist) {
}
}))
if (!all(numeric_or_normal)) {
stop(
"Delay distributions passed to the model need to have parameters that ",
"are either numeric or normally distributed with numeric parameters ",
"and infinite maximum."
cli_abort(
c(
"!" = "Delay distributions passed to the model need to have parameters
that are either {col_blue(\"numeric\")} or
{col_blue(\"normally distributed\")} with {col_blue(\"numeric\")}
parameters and {col_blue(\"infinite maximum\")}."
)
)
}
if (is.null(attr(dist, "tolerance"))) {
Expand All @@ -103,9 +110,12 @@ check_stan_delay <- function(dist) {
assert_numeric(attr(dist, "tolerance"), lower = 0, upper = 1)
# Check that `dist` has a finite maximum
if (any(is.infinite(max(dist))) && !(attr(dist, "tolerance") > 0)) {
stop(
"All distribution passed to the model need to have a finite maximum,",
"which can be achieved either by setting `max` or non-zero `tolerance`."
cli_abort(
c(
"i" = "All distribution passed to the model need to have a
{col_blue(\"finite maximum\")}, which can be achieved either by
setting {.var max} or non-zero {.var tolerance}."
)
)
}
}
Expand All @@ -117,19 +127,21 @@ check_stan_delay <- function(dist) {
#' @param pmf A probability mass function vector
#' @param span The number of consecutive indices in the tail to check
#' @param tol The value which to consider the tail as sparse
#' @importFrom cli cli_warn col_blue
#'
#' @return Called for its side effects.
#' @keywords internal
check_sparse_pmf_tail <- function(pmf, span = 5, tol = 1e-6) {
if (all(pmf[(length(pmf) - span + 1):length(pmf)] < tol)) {
warning(
sprintf(
"The PMF tail has %s consecutive values smaller than %s.",
span, tol
cli_warn(
c(
"!" = "The PMF tail has {col_blue(span)} consecutive value{?s} smaller
than {col_blue(tol)}.",
"i" = "This will drastically increase run times with very small
increases in accuracy. Consider increasing the tail values of the PMF."
),
" This will drastically increase run time with very small increases ",
"in accuracy. Consider increasing the tail values of the PMF.",
call. = FALSE
.frequency = "regularly",
.frequency_id = "sparse_pmf_tail"
)
}
}
7 changes: 6 additions & 1 deletion R/deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,18 @@ dist_skel <- function(n, dist = FALSE, cum = TRUE, model,
#' removed.
#' @return A `<dist_spec>` where probability masses below the threshold level
#' have been removed
#' @importFrom cli cli_abort
#' @keywords internal
apply_tolerance <- function(x, tolerance) {
lifecycle::deprecate_warn(
"1.6.0", "apply_tolerance()", "bound_dist()"
)
if (!is(x, "dist_spec")) {
stop("Can only apply tolerance to distributions in a <dist_spec>.")
cli_abort(
c(
"!" = "Can only apply tolerance to distributions in a {.cls dist_spec}."
)
)
}
y <- lapply(x, function(x) {
if (x$distribution == "nonparametric") {
Expand Down
Loading