Skip to content

Commit abf0a05

Browse files
committed
fix: adjust with Logan's suggestions
1 parent 13bfb3a commit abf0a05

File tree

6 files changed

+63
-58
lines changed

6 files changed

+63
-58
lines changed

R/autoplot.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ autoplot.epi_df <- function(
4747
.facet_by = c(".response", "other_keys", "all_keys", "geo_value", "all", "none"),
4848
.base_color = "#3A448F",
4949
.max_facets = Inf) {
50-
.color_by <- match.arg(.color_by)
51-
.facet_by <- match.arg(.facet_by)
50+
.color_by <- rlang::match_arg(.color_by)
51+
.facet_by <- rlang::match_arg(.facet_by)
5252

5353
assert(anyInfinite(.max_facets), checkInt(.max_facets), combine = "or")
5454
assert_character(.base_color, len = 1)

R/correlation.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ epi_cor <- function(x, var1, var2, dt1 = 0, dt2 = 0, shift_by = geo_value, # nol
9999
shift_by <- syms(names(eval_select(enquo(shift_by), x)))
100100

101101
# Which method?
102-
method <- match.arg(method)
102+
method <- rlang::match_arg(method)
103103

104104
# Perform time shifts, then compute appropriate correlations and return
105105
return(x %>%

R/growth_rate.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ growth_rate <- function(x = seq_along(y), y, x0 = x,
120120
# Check x, y, x0
121121
if (length(x) != length(y)) cli_abort("`x` and `y` must have the same length.")
122122
if (!all(x0 %in% x)) cli_abort("`x0` must be a subset of `x`.")
123-
method <- match.arg(method)
123+
method <- rlang::match_arg(method)
124124

125125
# Arrange in increasing order of x
126126
o <- order(x)

R/outliers.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ detect_outlr <- function(x = seq_along(y), y,
8989
),
9090
combiner = c("median", "mean", "none")) {
9191
# Validate combiner
92-
combiner <- match.arg(combiner)
92+
combiner <- rlang::match_arg(combiner)
9393

9494
# Validate that x contains all distinct values
9595
if (any(duplicated(x))) {

R/slide.R

+40-30
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
#' directly by name, the expression has access to `.data` and `.env` pronouns
2626
#' as in `dplyr` verbs, and can also refer to `.x`, `.group_key`, and
2727
#' `.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
2929
#' contain the derivative values. Default is "slide_value"; note that setting
3030
#' `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
3232
#' [unchopped][tidyr::unchop]/[unnested][tidyr::unnest]? Default is `FALSE`,
3333
#' in which case a list object returned by `f` would be unnested (using
3434
#' [`tidyr::unnest()`]), and, if the slide computations output data frames,
@@ -90,39 +90,49 @@
9090
#' ungroup()
9191
epi_slide <- function(
9292
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+
95105
assert_class(x, "epi_df")
96106

97107
if (nrow(x) == 0L) {
98108
return(x)
99109
}
100110

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)
103113
} 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))) {
106116
cli_abort(
107117
"`ref_time_values` must be a unique subset of the time values in `x`."
108118
)
109119
}
110-
if (anyDuplicated(ref_time_values) != 0L) {
120+
if (anyDuplicated(.ref_time_values) != 0L) {
111121
cli_abort("`ref_time_values` must not contain any duplicates; use `unique` if appropriate.")
112122
}
113123
}
114-
ref_time_values <- sort(ref_time_values)
124+
.ref_time_values <- sort(.ref_time_values)
115125

116126
if (!is.null(before) || !is.null(after)) {
117127
cli_abort("`before` and `after` are deprecated for `epi_slide`. Use `n` instead.")
118128
}
119129

120130
# Handle window arguments
121-
align <- match.arg(align)
131+
align <- rlang::arg_match(.align)
122132
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") {
126136
before <- Inf
127137
if (time_type %in% c("day", "week")) {
128138
after <- as.difftime(0, units = glue::glue("{time_type}s"))
@@ -131,32 +141,32 @@ epi_slide <- function(
131141
}
132142
} else {
133143
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."
135145
)
136146
}
137147
} else {
138-
if (align == "left") {
139-
before <- n - 1
148+
if (align == "right") {
149+
before <- .n - 1
140150
after <- 0
141151
} else if (align == "center") {
142152
# 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") {
146156
before <- 0
147-
after <- n - 1
157+
after <- .n - 1
148158
}
149159
}
150160

151161
# Arrange by increasing time_value
152162
x <- arrange(x, .data$time_value)
153163

154164
# 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
157167

158168
# Symbolize new column name
159-
new_col <- sym(new_col_name)
169+
new_col <- sym(.new_col_name)
160170

161171
# Computation for one group, all time values
162172
slide_one_grp <- function(.data_group,
@@ -211,7 +221,7 @@ epi_slide <- function(
211221

212222
# Unlist if appropriate:
213223
slide_values <-
214-
if (as_list_col) {
224+
if (.as_list_col) {
215225
slide_values_list
216226
} else {
217227
vctrs::list_unchop(slide_values_list)
@@ -228,7 +238,7 @@ epi_slide <- function(
228238
} else {
229239
# Split and flatten if appropriate, perform a (loose) check on number of
230240
# rows.
231-
if (as_list_col) {
241+
if (.as_list_col) {
232242
slide_values <- purrr::list_flatten(purrr::map(
233243
slide_values, ~ vctrs::vec_split(.x, seq_len(vctrs::vec_size(.x)))[["val"]]
234244
))
@@ -291,15 +301,15 @@ epi_slide <- function(
291301
f_factory = f_wrapper_factory,
292302
starts = starts,
293303
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,
296306
new_col = new_col,
297307
.keep = FALSE
298308
)
299309

300310
# 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)
303313
}
304314

305315
return(x)

man-roxygen/basic-slide-params.R

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
11
#' @param x The `epi_df` object under consideration, [grouped][dplyr::group_by]
22
#' or ungrouped. If ungrouped, all data in `x` will be treated as part of a
33
#' single data group.
4-
#' @param before,after How far `before` and `after` each `ref_time_value` should
5-
#' the sliding window extend? At least one of these two arguments must be
6-
#' provided; the other's default will be 0. The accepted values for these
7-
#' depend on the type of the `time_value` column:
4+
#' @param .n The size of the sliding window. By default, this is 0, meaning that
5+
#' only the current ref_time_value is included. The accepted values here depend
6+
#' on the `time_value` column:
87
#'
9-
#' - if it is a Date and the cadence is daily, then they can be integers
10-
#' (which will be interpreted in units of days) or difftimes with units
8+
#' - if time_type is Date and the cadence is daily, then `.n` can be an integer
9+
#' (which will be interpreted in units of days) or a difftime with units
1110
#' "days"
12-
#' - if it is a Date and the cadence is weekly, then they must be difftimes
11+
#' - if time_type is Date and the cadence is weekly, then `.n` must be a difftime
1312
#' with units "weeks"
14-
#' - if it is an integer, then they must be integers
13+
#' - if time_type is an integer, then `.n` must be an integer
1514
#'
16-
#' Endpoints of the window are inclusive. Common settings:
17-
#'
18-
#' - For trailing/right-aligned windows from `ref_time_value - k` to
19-
#' `ref_time_value`: either pass `before=k` by itself, or pass `before=k,
20-
#' after=0`.
21-
#' - For center-aligned windows from `ref_time_value - k` to
22-
#' `ref_time_value + k`: pass `before=k, after=k`.
23-
#' - For leading/left-aligned windows from `ref_time_value` to
24-
#' `ref_time_value + k`: either pass pass `after=k` by itself,
25-
#' or pass `before=0, after=k`.
26-
#'
27-
#' See "Details:" on how missing rows are handled within the window.
28-
#' @param ref_time_values Time values for sliding computations, meaning, each
15+
#' @param .align The alignment of the sliding window. If `right` (default), then
16+
#' the window has its end at the reference time; if `center`, then the window is
17+
#' centered at the reference time; if `left`, then the window has its start at
18+
#' the reference time. If the alignment is `center` and the window size is odd,
19+
#' then the window will have floor(window_size/2) points before and after the
20+
#' reference time. If the window size is even, then the window will be
21+
#' asymmetric and have one less value on the right side of the reference time.
22+
#' @param before,after Deprecated. Use `.n` instead.
23+
#' @param .ref_time_values Time values for sliding computations, meaning, each
2924
#' element of this vector serves as the reference time point for one sliding
3025
#' window. If missing, then this will be set to all unique time values in the
3126
#' underlying data table, by default.
32-
#' @param names_sep String specifying the separator to use in `tidyr::unnest()`
27+
#' @param .names_sep String specifying the separator to use in `tidyr::unnest()`
3328
#' when `as_list_col = FALSE`. Default is "_". Using `NULL` drops the prefix
3429
#' from `new_col_name` entirely.
35-
#' @param all_rows If `all_rows = TRUE`, then all rows of `x` will be kept in
30+
#' @param .all_rows If `all_rows = TRUE`, then all rows of `x` will be kept in
3631
#' the output even with `ref_time_values` provided, with some type of missing
3732
#' value marker for the slide computation output column(s) for `time_value`s
3833
#' outside `ref_time_values`; otherwise, there will be one row for each row in

0 commit comments

Comments
 (0)