We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
unique_check()
check_unique()
1 parent ad33275 commit 6b29cb9Copy full SHA for 6b29cb9
5 files changed
R/misc.R
@@ -297,3 +297,21 @@ check_frac_range <- function(x, ..., arg = caller_arg(x), call = caller_env()) {
297
call = call
298
)
299
}
300
+
301
+check_unique <- function(x, ..., arg = caller_arg(x), call = caller_env()) {
302
+ check_dots_empty()
303
+ x2 <- x[!is.na(x)]
304
+ is_dup <- duplicated(x2)
305
+ if (!any(is_dup)) {
306
+ return(invisible(NULL))
307
+ }
308
309
+ dup_list <- x2[is_dup]
310
+ cli::cli_abort(
311
+ c(
312
+ x = "{.arg {arg}} must have unique values.",
313
+ i = "Duplicates: {.val {dup_list}}"
314
+ ),
315
+ call = call
316
+ )
317
+}
R/parameters.R
@@ -66,25 +66,6 @@ parameters.list <- function(x, ...) {
66
67
68
69
-unique_check <- function(x, ..., call = caller_env()) {
70
- check_dots_empty()
71
- x2 <- x[!is.na(x)]
72
- is_dup <- duplicated(x2)
73
- if (any(is_dup)) {
74
- dup_list <- x2[is_dup]
75
- cl <- match.call()
76
-
77
- cli::cli_abort(
78
- c(
79
- x = "Element {.field {deparse(cl$x)}} should have unique values.",
80
- i = "Duplicates exist for {cli::qty(dup_list)} item{?s}: {dup_list}"
81
- ),
82
- call = call
83
- )
84
- }
85
- invisible(TRUE)
86
-}
87
88
param_or_na <- function(x) {
89
inherits(x, "param") | all(is.na(x))
90
@@ -135,7 +116,7 @@ parameters_constr <- function(
135
116
136
117
check_character(name, call = call)
137
118
check_character(id, call = call)
138
- unique_check(id, call = call)
119
+ check_unique(id, call = call)
139
120
check_character(source, call = call)
140
121
check_character(component, call = call)
141
122
check_character(component_id, call = call)
tests/testthat/_snaps/misc.md
@@ -174,6 +174,34 @@
174
Error:
175
! `c(0.1, NA)` must be a numeric vector of length 2 with values between 0 and 1, not a double vector.
176
177
+# check_unique() errors on duplicates
178
179
+ Code
180
+ check_unique(c("a", "a"))
181
+ Condition
182
+ Error:
183
+ x `c("a", "a")` must have unique values.
184
+ i Duplicates: "a"
185
186
+---
187
188
189
+ check_unique(c("a", "b", "a", "b"))
190
191
192
+ x `c("a", "b", "a", "b")` must have unique values.
193
+ i Duplicates: "a" and "b"
194
195
196
197
198
+ my_ids <- c("x", "x")
199
+ check_unique(my_ids)
200
201
202
+ x `my_ids` must have unique values.
203
+ i Duplicates: "x"
204
205
# vctrs-helpers-parameters
206
207
Code
tests/testthat/_snaps/parameters.md
@@ -13,8 +13,8 @@
13
parameters_constr(ab, id = c("a", "a"), ab, ab, ab)
14
Condition
15
16
- x Element id should have unique values.
17
- i Duplicates exist for item: a
+ x `id` must have unique values.
18
19
---
20
@@ -58,8 +58,8 @@
58
parameters(list(a = mtry(), a = penalty()))
59
60
Error in `parameters()`:
61
62
63
64
65
tests/testthat/test-misc.R
@@ -91,6 +91,27 @@ test_that("check_frac_range()", {
91
expect_snapshot(error = TRUE, check_frac_range(c(0.1, NA)))
92
})
93
94
+test_that("check_unique() passes with unique values", {
95
+ expect_null(check_unique(c("a", "b", "c")))
96
+ expect_null(check_unique(c(1, 2, 3)))
97
+ expect_null(check_unique(character()))
98
+})
99
100
+test_that("check_unique() ignores NA values", {
101
+ expect_null(check_unique(c("a", NA, "b")))
102
+ expect_null(check_unique(c(NA, NA, NA)))
103
+ expect_null(check_unique(c("a", NA, NA, "b")))
104
105
106
+test_that("check_unique() errors on duplicates", {
107
+ expect_snapshot(error = TRUE, check_unique(c("a", "a")))
108
+ expect_snapshot(error = TRUE, check_unique(c("a", "b", "a", "b")))
109
+ expect_snapshot(error = TRUE, {
110
111
112
+ })
113
114
115
test_that("vctrs-helpers-parameters", {
expect_false(dials:::is_parameters(2))
expect_snapshot(
0 commit comments