Skip to content

Commit 00434fa

Browse files
committed
Merge commit 'b56e9bc4878c60a1c8907cf092d7851690dc46f1'
2 parents d6fbce9 + b56e9bc commit 00434fa

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
* The `"optimal"` option for the `weight_func()` parameter has been removed since it is choosing the optimal value based on the resubstition error (#370).
1919

20+
* When constructing integer-valued parameters with a range of two consecutive values the `inclusive` argument needs to be set to `c(TRUE, TRUE)` to leave at least two values to sample from (#373).
21+
2022

2123
# dials 1.3.0
2224

R/constructors.R

+13
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ new_quant_param <- function(type = c("double", "integer"),
132132

133133
check_inclusive(inclusive, call = call)
134134

135+
if (type == "integer" && !any(is_unknown(range))) {
136+
range_of_2_consecutive_values <- identical(range[[1]] + 1L, range[[2]])
137+
if (range_of_2_consecutive_values) {
138+
if (!all(inclusive)) {
139+
cli::cli_abort(
140+
"{.arg inclusive} must be {.code c(TRUE, TRUE)} when the {.arg range}
141+
only covers two consecutive values.",
142+
call = call
143+
)
144+
}
145+
}
146+
}
147+
135148
if (!is.null(trans) && !is.trans(trans)) {
136149
cli::cli_abort(
137150
c(

tests/testthat/_snaps/constructors.md

+18
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@
105105
Error:
106106
! `finalize` must be a function or `NULL`, not the string "not a function or NULL".
107107

108+
# integer parameter: compatibility of `inclusive` and `range` (#373)
109+
110+
Code
111+
new_quant_param(type = "integer", range = c(0, 1), inclusive = c(FALSE, FALSE),
112+
trans = NULL, label = c(param_non_incl = "some label"), finalize = NULL)
113+
Condition
114+
Error:
115+
! `inclusive` must be `c(TRUE, TRUE)` when the `range` only covers two consecutive values.
116+
117+
---
118+
119+
Code
120+
new_quant_param(type = "integer", range = c(0, 1), inclusive = c(FALSE, TRUE),
121+
trans = NULL, label = c(param_non_incl = "some label"), finalize = NULL)
122+
Condition
123+
Error:
124+
! `inclusive` must be `c(TRUE, TRUE)` when the `range` only covers two consecutive values.
125+
108126
# bad args to range_validate
109127

110128
Code

tests/testthat/test-constructors.R

+35
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,41 @@ test_that("quantitative parameter object creation - bad args", {
5959
)
6060
})
6161

62+
test_that("integer parameter: compatibility of `inclusive` and `range` (#373)", {
63+
# if range covers only two consecutive integer values,
64+
# `inclusive = c(FALSE, FALSE)` would leave no values to sample from
65+
# and `inclusive = c(FALSE, TRUE)` would leave only one value
66+
expect_snapshot(error = TRUE, {
67+
new_quant_param(
68+
type = "integer",
69+
range = c(0, 1),
70+
inclusive = c(FALSE, FALSE),
71+
trans = NULL,
72+
label = c(param_non_incl = "some label"),
73+
finalize = NULL
74+
)
75+
})
76+
expect_snapshot(error = TRUE, {
77+
new_quant_param(
78+
type = "integer",
79+
range = c(0, 1),
80+
inclusive = c(FALSE, TRUE),
81+
trans = NULL,
82+
label = c(param_non_incl = "some label"),
83+
finalize = NULL
84+
)
85+
})
86+
expect_no_error({
87+
new_quant_param(
88+
type = "integer",
89+
range = c(0, 1),
90+
inclusive = c(TRUE, TRUE),
91+
trans = NULL,
92+
label = c(param_non_incl = "some label"),
93+
finalize = NULL
94+
)
95+
})
96+
})
6297

6398
test_that("bad args to range_validate", {
6499
expect_snapshot(

0 commit comments

Comments
 (0)