25
25
# ' directly by name, the expression has access to `.data` and `.env` pronouns
26
26
# ' as in `dplyr` verbs, and can also refer to `.x`, `.group_key`, and
27
27
# ' `.ref_time_value`. See details.
28
- # ' @param new_col_name String indicating the name of the new column that will
28
+ # ' @param . new_col_name String indicating the name of the new column that will
29
29
# ' contain the derivative values. Default is "slide_value"; note that setting
30
30
# ' `new_col_name` equal to an existing column name will overwrite this column.
31
- # ' @param as_list_col Should the slide results be held in a list column, or be
31
+ # ' @param . as_list_col Should the slide results be held in a list column, or be
32
32
# ' [unchopped][tidyr::unchop]/[unnested][tidyr::unnest]? Default is `FALSE`,
33
33
# ' in which case a list object returned by `f` would be unnested (using
34
34
# ' [`tidyr::unnest()`]), and, if the slide computations output data frames,
90
90
# ' ungroup()
91
91
epi_slide <- function (
92
92
x , f , ... ,
93
- n = 0 , align = c(" left" , " center" , " right" ), before = NULL , after = NULL , ref_time_values = NULL ,
94
- new_col_name = " slide_value" , as_list_col = FALSE , names_sep = " _" , all_rows = FALSE ) {
93
+ .n = 0 , .align = c(" right" , " center" , " left" ), .ref_time_values = NULL ,
94
+ .new_col_name = " slide_value" , .as_list_col = FALSE , .names_sep = " _" , .all_rows = FALSE ,
95
+ before = NULL , after = NULL , ref_time_values = NULL , new_col_name = NULL ,
96
+ as_list_col = NULL , names_sep = NULL , all_rows = NULL ) {
97
+ if (any(map(c(n , before , after , ref_time_values , new_col_name , as_list_col , names_sep , all_rows ), Negate(is.null )))) {
98
+ cli_abort(
99
+ " epi_slide: deprecated arguments `n`, `before`, `after`, `ref_time_values`, `new_col_name`, `as_list_col`,
100
+ `names_sep`, and `all_rows` have been removed. Please use `.n`, `.align`, `.ref_time_values`,
101
+ `.new_col_name`, `.as_list_col`, and `.names_sep` instead."
102
+ )
103
+ }
104
+
95
105
assert_class(x , " epi_df" )
96
106
97
107
if (nrow(x ) == 0L ) {
98
108
return (x )
99
109
}
100
110
101
- if (is.null(ref_time_values )) {
102
- ref_time_values <- unique(x $ time_value )
111
+ if (is.null(. ref_time_values )) {
112
+ . ref_time_values <- unique(x $ time_value )
103
113
} else {
104
- assert_numeric(ref_time_values , min.len = 1L , null.ok = FALSE , any.missing = FALSE )
105
- if (! test_subset(ref_time_values , unique(x $ time_value ))) {
114
+ assert_numeric(. ref_time_values , min.len = 1L , null.ok = FALSE , any.missing = FALSE )
115
+ if (! test_subset(. ref_time_values , unique(x $ time_value ))) {
106
116
cli_abort(
107
117
" `ref_time_values` must be a unique subset of the time values in `x`."
108
118
)
109
119
}
110
- if (anyDuplicated(ref_time_values ) != 0L ) {
120
+ if (anyDuplicated(. ref_time_values ) != 0L ) {
111
121
cli_abort(" `ref_time_values` must not contain any duplicates; use `unique` if appropriate." )
112
122
}
113
123
}
114
- ref_time_values <- sort(ref_time_values )
124
+ . ref_time_values <- sort(. ref_time_values )
115
125
116
126
if (! is.null(before ) || ! is.null(after )) {
117
127
cli_abort(" `before` and `after` are deprecated for `epi_slide`. Use `n` instead." )
118
128
}
119
129
120
130
# Handle window arguments
121
- align <- match.arg( align )
131
+ align <- rlang :: arg_match( . align )
122
132
time_type <- attr(x , " metadata" )$ time_type
123
- validate_slide_window_arg(n , time_type )
124
- if (n == Inf ) {
125
- if (align == " left " ) {
133
+ validate_slide_window_arg(. n , time_type )
134
+ if (identical( .n , Inf ) ) {
135
+ if (align == " right " ) {
126
136
before <- Inf
127
137
if (time_type %in% c(" day" , " week" )) {
128
138
after <- as.difftime(0 , units = glue :: glue(" {time_type}s" ))
@@ -131,32 +141,32 @@ epi_slide <- function(
131
141
}
132
142
} else {
133
143
cli_abort(
134
- " `epi_slide`: center and right alignment are not supported with an infinite window size."
144
+ " `epi_slide`: center and left alignment are not supported with an infinite window size."
135
145
)
136
146
}
137
147
} else {
138
- if (align == " left " ) {
139
- before <- n - 1
148
+ if (align == " right " ) {
149
+ before <- . n - 1
140
150
after <- 0
141
151
} else if (align == " center" ) {
142
152
# For n = 5, before = 2, after = 2. For n = 4, before = 2, after = 1.
143
- before <- floor(n / 2 )
144
- after <- n - before - 1
145
- } else if (align == " right " ) {
153
+ before <- floor(. n / 2 )
154
+ after <- . n - before - 1
155
+ } else if (align == " left " ) {
146
156
before <- 0
147
- after <- n - 1
157
+ after <- . n - 1
148
158
}
149
159
}
150
160
151
161
# Arrange by increasing time_value
152
162
x <- arrange(x , .data $ time_value )
153
163
154
164
# Now set up starts and stops for sliding/hopping
155
- starts <- ref_time_values - before
156
- stops <- ref_time_values + after
165
+ starts <- . ref_time_values - before
166
+ stops <- . ref_time_values + after
157
167
158
168
# Symbolize new column name
159
- new_col <- sym(new_col_name )
169
+ new_col <- sym(. new_col_name )
160
170
161
171
# Computation for one group, all time values
162
172
slide_one_grp <- function (.data_group ,
@@ -211,7 +221,7 @@ epi_slide <- function(
211
221
212
222
# Unlist if appropriate:
213
223
slide_values <-
214
- if (as_list_col ) {
224
+ if (. as_list_col ) {
215
225
slide_values_list
216
226
} else {
217
227
vctrs :: list_unchop(slide_values_list )
@@ -228,7 +238,7 @@ epi_slide <- function(
228
238
} else {
229
239
# Split and flatten if appropriate, perform a (loose) check on number of
230
240
# rows.
231
- if (as_list_col ) {
241
+ if (. as_list_col ) {
232
242
slide_values <- purrr :: list_flatten(purrr :: map(
233
243
slide_values , ~ vctrs :: vec_split(.x , seq_len(vctrs :: vec_size(.x )))[[" val" ]]
234
244
))
@@ -291,15 +301,15 @@ epi_slide <- function(
291
301
f_factory = f_wrapper_factory ,
292
302
starts = starts ,
293
303
stops = stops ,
294
- ref_time_values = ref_time_values ,
295
- all_rows = all_rows ,
304
+ ref_time_values = . ref_time_values ,
305
+ all_rows = . all_rows ,
296
306
new_col = new_col ,
297
307
.keep = FALSE
298
308
)
299
309
300
310
# Unnest if we need to, and return
301
- if (! as_list_col ) {
302
- x <- unnest(x , !! new_col , names_sep = names_sep )
311
+ if (! . as_list_col ) {
312
+ x <- unnest(x , !! new_col , names_sep = . names_sep )
303
313
}
304
314
305
315
return (x )
0 commit comments