Skip to content
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

No more cat() in printing methods #353

Merged
merged 8 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
hfrick marked this conversation as resolved.
Show resolved Hide resolved

# dials 1.3.0

## Improvements
Expand Down
30 changes: 10 additions & 20 deletions R/constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ print.quant_param <- function(x, digits = 3, ...) {

cat_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")
}
}

Expand All @@ -237,7 +237,7 @@ cat_quant_param_range <- function(x) {
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) {
Expand All @@ -249,25 +249,22 @@ 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) {
if (!is.null(x$trans)) {
print(eval(x$trans))
text <- utils::capture.output(eval(x$trans))
hfrick marked this conversation as resolved.
Show resolved Hide resolved
cli::cli_text(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:")
Expand All @@ -276,14 +273,7 @@ print.qual_param <- function(x, ...) {
} else {
lvls <- x$values
}
cat(
glue_collapse(
lvls,
sep = ", ",
last = " and ",
width = options()$width
),
"\n"
)

cli::cli_text("{lvls}")
invisible(x)
}
64 changes: 29 additions & 35 deletions R/parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,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()
hfrick marked this conversation as resolved.
Show resolved Hide resolved

print_x <- x %>% dplyr::select(identifier = id, type = name, object)
print_x$object <-
Expand All @@ -176,35 +178,21 @@ print.parameters <- function(x, ...) {
pillar::type_sum(.x)
}
)

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

cli::cli_par()
cli::cli_verbatim(
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
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 `"
}
needs_param <- print_x$identifier[null_obj]

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."
)
hfrick marked this conversation as resolved.
Show resolved Hide resolved
cli::cli_text(
"The parameter{?s} {.var {needs_param}} {?needs a/need} {.var param}
{?object/objects}. See {.code vignette('dials')} to learn more."
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -220,23 +208,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'
hfrick marked this conversation as resolved.
Show resolved Hide resolved
'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 @@ -78,63 +78,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. See `vignette('dials')` to learn
more.

---

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. See
`vignette('dials')` to learn more.

---

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. See
`vignette('dials')` to learn more.

# parameters.default

Expand Down
Loading