Skip to content

Commit

Permalink
Merge pull request #353 from tidymodels/no-more-cat
Browse files Browse the repository at this point in the history
No more `cat()` in printing methods
  • Loading branch information
hfrick authored Sep 24, 2024
2 parents 555493b + 3c54d11 commit e2465f6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 86 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dials (development version)

* All messages, warnings and errors has been translated to use {cli} package (#311).

# dials 1.3.0

## Improvements
Expand Down
47 changes: 19 additions & 28 deletions R/constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,31 +216,31 @@ new_qual_param <- function(type = c("character", "logical"),

#' @export
print.quant_param <- function(x, digits = 3, ...) {
cat_quant_param_header(x)
print_transformer(x)
cat_quant_param_range(x)
cat_quant_param_values(x)
print_quant_param_header(x)
print_quant_param_transformer(x)
print_quant_param_range(x)
print_quant_param_values(x)
invisible(x)
}

cat_quant_param_header <- function(x) {
print_quant_param_header <- function(x) {
if (!is.null(x$label)) {
cat_line(x$label, " (quantitative)")
cli::cli_text("{x$label} (quantitative)")
} else {
cat_line("Quantitative Parameter")
cli::cli_text("Quantitative Parameter")
}
}

cat_quant_param_range <- function(x) {
print_quant_param_range <- function(x) {
label <- format_range_label(x, "Range")

range <- map_chr(x$range, format_range_val)
range <- format_range(x, range)

cat_line(label, range)
cli::cli_text("{label}{range}")
}

cat_quant_param_values <- function(x) {
print_quant_param_values <- function(x) {
values <- x$values

if (is.null(values)) {
Expand All @@ -249,41 +249,32 @@ cat_quant_param_values <- function(x) {

n_values <- length(values)

cat_line(glue("Values: {n_values}"))
cli::cli_text("Values: {n_values}")
}

print_transformer <- function(x) {
print_quant_param_transformer <- function(x) {
if (!is.null(x$trans)) {
print(eval(x$trans))
text <- utils::capture.output(eval(x$trans))
cli::cli_verbatim(text)
}
}

cat_line <- function(...) {
cat(paste0(..., "\n", collapse = ""))
}

#' @export
print.qual_param <- function(x, ...) {
if (!is.null(x$label)) {
cat(x$label, " (qualitative)\n")
cli::cli_text("{x$label} (qualitative)")
} else {
cat("Qualitative Parameter\n")
cli::cli_text("Qualitative Parameter")
}

n_values <- length(x$values)
cli::cli_text("{n_values} possible value{?s} include:")
if (x$type == "character") {
lvls <- paste0("'", x$values, "'")
} else {
lvls <- x$values
}
cat(
glue_collapse(
lvls,
sep = ", ",
last = " and ",
width = options()$width
),
"\n"
)
cli::cli_text("{lvls}")

invisible(x)
}
67 changes: 31 additions & 36 deletions R/parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ unk_check <- function(x) {
print.parameters <- function(x, ...) {
x <- tibble::as_tibble(x)

cat("Collection of", nrow(x), "parameters for tuning\n\n")
cli::cli_par()
cli::cli_text("Collection of {nrow(x)} parameters for tuning")
cli::cli_end()

print_x <- x %>% dplyr::select(identifier = id, type = name, object)
print_x$object <-
Expand All @@ -177,36 +179,23 @@ print.parameters <- function(x, ...) {
pillar::type_sum(.x)
}
)

print.data.frame(print_x, row.names = FALSE)
cat("\n")

cli::cli_par()
cli::cli_verbatim(
utils::capture.output(print.data.frame(print_x, row.names = FALSE))
)
cli::cli_end()

null_obj <- map_lgl(x$object, ~ all(is.na(.x)))

if (any(null_obj)) {
needs_param <- print_x$identifier[null_obj]

last_sep <- if (length(needs_param) == 2) {
"` and `"
} else {
"`, and `"
}

param_descs <- paste0(
"`",
glue::glue_collapse(print_x$identifier[null_obj], sep = "`, `", last = last_sep),
"`"
)

plural <- length(needs_param) != 1

rlang::inform(
glue::glue(
"The parameter{if (plural) 's' else ''} {param_descs} ",
"{if (plural) {'need `param` objects'} else {'needs a `param` object'}}. ",
"\nSee `vignette('dials')` to learn more."
)
needs_param <- print_x$identifier[null_obj]
cli::cli_par()
cli::cli_text(
"The parameter{?s} {.var {needs_param}} {?needs a/need} {.cls param}
{?object/objects}."
)
cli::cli_end()
}

other_obj <-
Expand All @@ -221,23 +210,29 @@ print.parameters <- function(x, ...) {
# There's a more elegant way to do this, I'm sure:
mod_obj <- as_tibble(other_obj) %>% dplyr::filter(source == "model_spec" & not_final)
if (nrow(mod_obj) > 0) {
cat("Model parameters needing finalization:\n")
cat(mod_obj$note, sep = "")
cat("\n")
cli::cli_par()
cli::cli_text("Model parameters needing finalization:")
cli::cli_text("{mod_obj$note}")
cli::cli_end()
}
rec_obj <- as_tibble(other_obj) %>% dplyr::filter(source == "recipe" & not_final)
if (nrow(rec_obj) > 0) {
cat("Recipe parameters needing finalization:\n")
cat(rec_obj$note, sep = "")
cat("\n")
cli::cli_par()
cli::cli_text("Recipe parameters needing finalization:")
cli::cli_text("{rec_obj$note}")
cli::cli_end()
}
lst_obj <- as_tibble(other_obj) %>% dplyr::filter(source == "list" & not_final)
if (nrow(lst_obj) > 0) {
cat("Parameters needing finalization:\n")
cat(lst_obj$note, sep = "")
cat("\n")
cli::cli_par()
cli::cli_text("Parameters needing finalization:")
cli::cli_text("{lst_obj$note}")
cli::cli_end()
}
cat("See `?dials::finalize` or `?dials::update.parameters` for more information.\n\n")
cli::cli_text(
"See {.help dials::finalize} or {.help dials::update.parameters} for
more information."
)
}

invisible(x)
Expand Down
11 changes: 5 additions & 6 deletions tests/testthat/_snaps/constructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,25 @@

Code
mtry()
Output
Message
# Randomly Selected Predictors (quantitative)
Range: [1, ?]

---

Code
surv_dist()
Output
Distribution (qualitative)
Message
Distribution (qualitative)
6 possible values include:
Output
'weibull', 'exponential', 'gaussian', 'logistic', 'lognormal' and 'loglogistic'
'weibull', 'exponential', 'gaussian', 'logistic', 'lognormal', and
'loglogistic'

---

Code
value_set(cost_complexity(), log10(c(0.09, 1e-04)))
Output
Message
Cost-Complexity Parameter (quantitative)
Transformer: log-10 [1e-100, Inf]
Range (transformed scale): [-10, -1]
Expand Down
28 changes: 12 additions & 16 deletions tests/testthat/_snaps/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,63 +104,59 @@

Code
parameters(list(mtry(), penalty()))
Output
Message
Collection of 2 parameters for tuning
identifier type object
mtry mtry nparam[?]
penalty penalty nparam[+]
Parameters needing finalization:
# Randomly Selected Predictors ('mtry')
See `?dials::finalize` or `?dials::update.parameters` for more information.
# Randomly Selected Predictors ('mtry')
See `?dials::finalize()` or `?dials::update.parameters()` for more information.

---

Code
ex_params[1, ] %>% structure(class = c("parameters", class(.)))
Output
Message
Collection of 1 parameters for tuning
identifier type object
trials trials missing
Message
The parameter `trials` needs a `param` object.
See `vignette('dials')` to learn more.
The parameter `trials` needs a <param> object.

---

Code
ex_params[1:2, ] %>% structure(class = c("parameters", class(.)))
Output
Message
Collection of 2 parameters for tuning
identifier type object
trials trials missing
rules rules missing
Message
The parameters `trials` and `rules` need `param` objects.
See `vignette('dials')` to learn more.
The parameters `trials` and `rules` need <param> objects.

---

Code
ex_params[1:3, ] %>% structure(class = c("parameters", class(.)))
Output
Message
Collection of 3 parameters for tuning
identifier type object
trials trials missing
rules rules missing
costs costs missing
Message
The parameters `trials`, `rules`, and `costs` need `param` objects.
See `vignette('dials')` to learn more.
The parameters `trials`, `rules`, and `costs` need <param> objects.

# parameters.default

Expand Down

0 comments on commit e2465f6

Please sign in to comment.