Skip to content

Commit

Permalink
Merge pull request #376 from tidymodels/empty-label
Browse files Browse the repository at this point in the history
Constructors fill in missing label
  • Loading branch information
hfrick authored Feb 12, 2025
2 parents 35c028b + 39446b3 commit da8400f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* For space-filling designs for $p$ parameters, there is a higher likelihood of finding a space-filling design for `1 < size <= p`. Also, single-point designs now default to a random grid (#363).

* The constructors, `new_*_parameter()`, now label unlabeled parameter (i.e., constructed with `label = NULL`) as such (#349).

## Breaking changes

* The `grid_*()` functions now error instead of warn when provided with the wrong argument to control the grid size. So `grid_space_filling()`, `grid_random()`, `grid_max_entropy()`, and `grid_latin_hypercube()` now error if used with a `levels` argument and `grid_regular()` now errors if used with a `size` argument (#368).
Expand Down
56 changes: 36 additions & 20 deletions R/constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
#' If set, these will be used by [value_seq()] and [value_sample()].
#'
#' @param label An optional named character string that can be used for
#' printing and plotting. The name should match the object name (e.g.
#' `"mtry"`, `"neighbors"`, etc.)
#' printing and plotting. The name of the label should match the object name
#' (e.g., `"mtry"`, `"neighbors"`, etc.). If `NULL`, the parameter will be
#' labeled with `"Unlabeled parameter"`.
#'
#' @param finalize A function that can be used to set the data-specific
#' values of a parameter (such as the `range`).
Expand Down Expand Up @@ -76,16 +77,18 @@ NULL

#' @export
#' @rdname new-param
new_quant_param <- function(type = c("double", "integer"),
range = NULL,
inclusive = NULL,
default = deprecated(),
trans = NULL,
values = NULL,
label = NULL,
finalize = NULL,
...,
call = caller_env()) {
new_quant_param <- function(
type = c("double", "integer"),
range = NULL,
inclusive = NULL,
default = deprecated(),
trans = NULL,
values = NULL,
label = NULL,
finalize = NULL,
...,
call = caller_env()
) {
if (lifecycle::is_present(default)) {
lifecycle::deprecate_stop(
when = "1.1.0",
Expand Down Expand Up @@ -156,6 +159,11 @@ new_quant_param <- function(type = c("double", "integer"),
}

check_label(label, call = call)
if (is.null(label)) {
label = "Unlabeled parameter"
names(label) <- "Unlabeled parameter"
}

check_function(finalize, allow_null = TRUE, call = call)

names(range) <- names(inclusive) <- c("lower", "upper")
Expand Down Expand Up @@ -189,16 +197,18 @@ new_quant_param <- function(type = c("double", "integer"),

#' @export
#' @rdname new-param
new_qual_param <- function(type = c("character", "logical"),
values,
default = deprecated(),
label = NULL,
finalize = NULL,
...,
call = caller_env()) {
new_qual_param <- function(
type = c("character", "logical"),
values,
default = deprecated(),
label = NULL,
finalize = NULL,
...,
call = caller_env()
) {
if (lifecycle::is_present(default)) {
lifecycle::deprecate_stop(
when = "1.1.0",
when = "1.1.0",
what = "new_qual_param(default)"
)
}
Expand All @@ -210,7 +220,13 @@ new_qual_param <- function(type = c("character", "logical"),
"logical" = check_logical(values, call = call),
"character" = check_character(values, call = call)
)

check_label(label, call = call)
if (is.null(label)) {
label = "Unlabeled parameter"
names(label) <- "Unlabeled parameter"
}

check_function(finalize, allow_null = TRUE, call = call)

res <- list(
Expand Down
5 changes: 3 additions & 2 deletions man/new-param.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da8400f

Please sign in to comment.