@@ -53,41 +53,24 @@ grouped_epi_archive <-
53
53
public = list (
54
54
initialize = function (ungrouped , vars , drop ) {
55
55
if (inherits(ungrouped , " grouped_epi_archive" )) {
56
- Abort (" `ungrouped` must not already be grouped (neither automatic regrouping nor nested grouping is supported). Either use `group_by` with `.add=TRUE`, or `ungroup` first." ,
56
+ cli_abort (" `ungrouped` must not already be grouped (neither automatic regrouping nor nested grouping is supported). Either use `group_by` with `.add=TRUE`, or `ungroup` first." ,
57
57
class = " epiprocess__grouped_epi_archive__ungrouped_arg_is_already_grouped" ,
58
58
epiprocess__ungrouped_class = class(ungrouped ),
59
59
epiprocess__ungrouped_groups = groups(ungrouped )
60
60
)
61
61
}
62
- if (! inherits(ungrouped , " epi_archive" )) {
63
- Abort(" `ungrouped` must be an epi_archive" ,
64
- class = " epiprocess__grouped_epi_archive__ungrouped_arg_is_not_epi_archive" ,
65
- epiprocess__ungrouped_class = class(ungrouped )
66
- )
67
- }
68
- if (! is.character(vars )) {
69
- Abort(" `vars` must be a character vector (any tidyselection should have already occurred in a helper method)." ,
70
- class = " epiprocess__grouped_epi_archive__vars_is_not_chr" ,
71
- epiprocess__vars_class = class(vars ),
72
- epiprocess__vars_type = typeof(vars )
73
- )
74
- }
75
- if (! all(vars %in% names(ungrouped $ DT ))) {
76
- Abort(" `vars` must be selected from the names of columns of `ungrouped$DT`" ,
77
- class = " epiprocess__grouped_epi_archive__vars_contains_invalid_entries" ,
78
- epiprocess__vars = vars ,
79
- epiprocess__DT_names = names(ungrouped $ DT )
62
+ assert_class(ungrouped , " epi_archive" )
63
+ assert_character(vars )
64
+ if (! test_subset(vars , names(ungrouped $ DT ))) {
65
+ cli_abort(
66
+ " All grouping variables `vars` must be present in the data." ,
80
67
)
81
68
}
82
69
if (" version" %in% vars ) {
83
- Abort(" `version` has a special interpretation and cannot be used by itself as a grouping variable" )
84
- }
85
- if (! rlang :: is_bool(drop )) {
86
- Abort(" `drop` must be a Boolean" ,
87
- class = " epiprocess__grouped_epi_archive__drop_is_not_bool" ,
88
- epiprocess__drop = drop
89
- )
70
+ cli_abort(" `version` has a special interpretation and cannot be used by itself as a grouping variable" )
90
71
}
72
+ assert_logical(drop , len = 1 )
73
+
91
74
# -----
92
75
private $ ungrouped <- ungrouped
93
76
private $ vars <- vars
@@ -136,11 +119,9 @@ grouped_epi_archive <-
136
119
invisible (self )
137
120
},
138
121
group_by = function (... , .add = FALSE , .drop = dplyr :: group_by_drop_default(self )) {
139
- if (! rlang :: is_bool(.add )) {
140
- Abort(" `.add` must be a Boolean" )
141
- }
122
+ assert_logical(.add , len = 1 )
142
123
if (! .add ) {
143
- Abort (' `group_by` on a `grouped_epi_archive` with `.add=FALSE` is forbidden
124
+ cli_abort (' `group_by` on a `grouped_epi_archive` with `.add=FALSE` is forbidden
144
125
(neither automatic regrouping nor nested grouping is supported).
145
126
If you want to "regroup", replacing the existing grouping vars, `ungroup` first and then `group_by`.
146
127
If you want to add to the existing grouping vars, call `group_by` specifying `.add=TRUE`.
@@ -210,7 +191,7 @@ grouped_epi_archive <-
210
191
# early development versions and much more likely to be clutter than
211
192
# informative in the signature.
212
193
if (" group_by" %in% nse_dots_names(... )) {
213
- Abort ("
194
+ cli_abort ("
214
195
The `group_by` argument to `slide` has been removed; please use
215
196
the `group_by` S3 generic function or `$group_by` R6 method
216
197
before the slide instead. (If you were instead trying to pass a
@@ -221,7 +202,7 @@ grouped_epi_archive <-
221
202
" , class = " epiprocess__epix_slide_group_by_parameter_deprecated" )
222
203
}
223
204
if (" all_rows" %in% nse_dots_names(... )) {
224
- Abort ("
205
+ cli_abort ("
225
206
The `all_rows` argument has been removed from `epix_slide` (but
226
207
is still supported in `epi_slide`). Add rows for excluded
227
208
results with a manual join instead.
@@ -230,32 +211,29 @@ grouped_epi_archive <-
230
211
231
212
if (missing(ref_time_values )) {
232
213
ref_time_values <- epix_slide_ref_time_values_default(private $ ungrouped )
233
- } else if (length(ref_time_values ) == 0L ) {
234
- Abort(" `ref_time_values` must have at least one element." )
235
- } else if (any(is.na(ref_time_values ))) {
236
- Abort(" `ref_time_values` must not include `NA`." )
237
- } else if (anyDuplicated(ref_time_values ) != 0L ) {
238
- Abort(" `ref_time_values` must not contain any duplicates; use `unique` if appropriate." )
239
- } else if (any(ref_time_values > private $ ungrouped $ versions_end )) {
240
- Abort(" All `ref_time_values` must be `<=` the `versions_end`." )
241
214
} else {
215
+ assert_numeric(ref_time_values , min.len = 1L , null.ok = FALSE , any.missing = FALSE )
216
+ if (any(ref_time_values > private $ ungrouped $ versions_end )) {
217
+ cli_abort(" Some `ref_time_values` are greater than the latest version in the archive." )
218
+ }
219
+ if (anyDuplicated(ref_time_values ) != 0L ) {
220
+ cli_abort(" Some `ref_time_values` are duplicated." )
221
+ }
242
222
# Sort, for consistency with `epi_slide`, although the current
243
223
# implementation doesn't take advantage of it.
244
224
ref_time_values <- sort(ref_time_values )
245
225
}
246
226
247
227
# Validate and pre-process `before`:
248
228
if (missing(before )) {
249
- Abort (" `before` is required (and must be passed by name);
229
+ cli_abort (" `before` is required (and must be passed by name);
250
230
if you did not want to apply a sliding window but rather
251
231
to map `as_of` and `f` across various `ref_time_values`,
252
232
pass a large `before` value (e.g., if time steps are days,
253
233
`before=365000`)." )
254
234
}
255
235
before <- vctrs :: vec_cast(before , integer())
256
- if (length(before ) != 1L || is.na(before ) || before < 0L ) {
257
- Abort(" `before` must be length-1, non-NA, non-negative." )
258
- }
236
+ assert_int(before , lower = 0L , null.ok = FALSE , na.ok = FALSE )
259
237
260
238
# If a custom time step is specified, then redefine units
261
239
@@ -265,15 +243,9 @@ grouped_epi_archive <-
265
243
new_col <- sym(new_col_name )
266
244
267
245
# Validate rest of parameters:
268
- if (! rlang :: is_bool(as_list_col )) {
269
- Abort(" `as_list_col` must be TRUE or FALSE." )
270
- }
271
- if (! (rlang :: is_string(names_sep ) || is.null(names_sep ))) {
272
- Abort(" `names_sep` must be a (single) string or NULL." )
273
- }
274
- if (! rlang :: is_bool(all_versions )) {
275
- Abort(" `all_versions` must be TRUE or FALSE." )
276
- }
246
+ assert_logical(as_list_col , len = 1L )
247
+ assert_logical(all_versions , len = 1L )
248
+ assert_character(names_sep , len = 1L , null.ok = TRUE )
277
249
278
250
# Computation for one group, one time value
279
251
comp_one_grp <- function (.data_group , .group_key ,
@@ -290,9 +262,7 @@ grouped_epi_archive <-
290
262
.data_group <- .data_group $ DT
291
263
}
292
264
293
- if (! (is.atomic(comp_value ) || is.data.frame(comp_value ))) {
294
- Abort(" The slide computation must return an atomic vector or a data frame." )
295
- }
265
+ assert(check_atomic(comp_value , any.missing = TRUE ), check_data_frame(comp_value ), combine = " or" , .var.name = vname(comp_value ))
296
266
297
267
# Label every result row with the `ref_time_value`
298
268
res <- list (time_value = ref_time_value )
@@ -312,10 +282,10 @@ grouped_epi_archive <-
312
282
if (missing(f )) {
313
283
quos <- enquos(... )
314
284
if (length(quos ) == 0 ) {
315
- Abort (" If `f` is missing then a computation must be specified via `...`." )
285
+ cli_abort (" If `f` is missing then a computation must be specified via `...`." )
316
286
}
317
287
if (length(quos ) > 1 ) {
318
- Abort (" If `f` is missing then only a single computation can be specified via `...`." )
288
+ cli_abort (" If `f` is missing then only a single computation can be specified via `...`." )
319
289
}
320
290
321
291
f <- quos [[1 ]]
0 commit comments