Skip to content

Commit 5958a76

Browse files
authored
Merge pull request #564 from cmu-delphi/lcb/epi_slide_opt-output-renaming
Adjust `epi_slide_opt` output naming; adjust `epi_slide*` autogrouping; fix tidyselect issue
2 parents 17d22d9 + 7bf4c6a commit 5958a76

31 files changed

+1106
-699
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: epiprocess
22
Type: Package
33
Title: Tools for basic signal processing in epidemiology
4-
Version: 0.9.6
4+
Version: 0.9.7
55
Authors@R: c(
66
person("Jacob", "Bien", role = "ctb"),
77
person("Logan", "Brooks", , "[email protected]", role = c("aut", "cre")),

NAMESPACE

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ S3method(arrange_row_canonical,default)
1111
S3method(arrange_row_canonical,epi_df)
1212
S3method(as_epi_df,data.frame)
1313
S3method(as_epi_df,epi_df)
14+
S3method(as_epi_df,grouped_df)
1415
S3method(as_epi_df,tbl_df)
1516
S3method(as_epi_df,tbl_ts)
1617
S3method(as_tibble,epi_df)
@@ -108,12 +109,16 @@ importFrom(checkmate,assert)
108109
importFrom(checkmate,assert_character)
109110
importFrom(checkmate,assert_class)
110111
importFrom(checkmate,assert_data_frame)
112+
importFrom(checkmate,assert_false)
111113
importFrom(checkmate,assert_function)
112114
importFrom(checkmate,assert_int)
113115
importFrom(checkmate,assert_list)
114116
importFrom(checkmate,assert_logical)
115117
importFrom(checkmate,assert_numeric)
116118
importFrom(checkmate,assert_scalar)
119+
importFrom(checkmate,assert_string)
120+
importFrom(checkmate,assert_subset)
121+
importFrom(checkmate,assert_tibble)
117122
importFrom(checkmate,checkInt)
118123
importFrom(checkmate,check_atomic)
119124
importFrom(checkmate,check_data_frame)
@@ -163,6 +168,7 @@ importFrom(dplyr,groups)
163168
importFrom(dplyr,if_all)
164169
importFrom(dplyr,if_any)
165170
importFrom(dplyr,if_else)
171+
importFrom(dplyr,is_grouped_df)
166172
importFrom(dplyr,lag)
167173
importFrom(dplyr,mutate)
168174
importFrom(dplyr,near)
@@ -176,6 +182,7 @@ importFrom(dplyr,summarize)
176182
importFrom(dplyr,tibble)
177183
importFrom(dplyr,ungroup)
178184
importFrom(ggplot2,autoplot)
185+
importFrom(glue,glue)
179186
importFrom(lifecycle,deprecated)
180187
importFrom(lubridate,as.period)
181188
importFrom(lubridate,days)
@@ -189,7 +196,6 @@ importFrom(rlang,"%||%")
189196
importFrom(rlang,.data)
190197
importFrom(rlang,.env)
191198
importFrom(rlang,arg_match)
192-
importFrom(rlang,as_label)
193199
importFrom(rlang,caller_arg)
194200
importFrom(rlang,caller_env)
195201
importFrom(rlang,check_dots_empty)
@@ -199,6 +205,7 @@ importFrom(rlang,env)
199205
importFrom(rlang,expr_label)
200206
importFrom(rlang,f_env)
201207
importFrom(rlang,f_rhs)
208+
importFrom(rlang,is_bare_integerish)
202209
importFrom(rlang,is_environment)
203210
importFrom(rlang,is_formula)
204211
importFrom(rlang,is_function)
@@ -207,7 +214,7 @@ importFrom(rlang,is_quosure)
207214
importFrom(rlang,list2)
208215
importFrom(rlang,missing_arg)
209216
importFrom(rlang,new_function)
210-
importFrom(rlang,quo_get_expr)
217+
importFrom(rlang,quo_get_env)
211218
importFrom(rlang,quo_is_missing)
212219
importFrom(rlang,sym)
213220
importFrom(rlang,syms)
@@ -232,3 +239,5 @@ importFrom(tidyselect,starts_with)
232239
importFrom(tsibble,as_tsibble)
233240
importFrom(utils,capture.output)
234241
importFrom(utils,tail)
242+
importFrom(vctrs,vec_data)
243+
importFrom(vctrs,vec_equal)

NEWS.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
1414
with `covid`. The data set previously named `jhu_confirmed_cumulative_num` has
1515
been removed from the package, but a renamed version is has been removed from
1616
the package, but a renamed version is still available in `epidatasets`.
17-
18-
## Bug fixes
19-
20-
- Removed `.window_size = 1` default from `epi_slide_{mean,sum,opt}`; this
21-
argument is now mandatory, and should nearly always be greater than 1 except
22-
for testing purposes.
17+
- `epi_slide_{sum,mean,opt}` have improved default output column names, and
18+
additional arguments for specifying names: `.prefix`, `.suffix`,
19+
`.new_col_names`. To obtain the old naming behavior, use `.prefix =
20+
"slide_value_"`.
21+
- `as_epi_df` now removes any grouping that `x` had applied.
2322

2423
## Improvements
2524

@@ -29,6 +28,19 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
2928
- Improved validation of `.window_size` arguments.
3029
- Rewrote a lot of the package documentation to be more consistent and
3130
informative. Simplified and streamlined the vignettes.
31+
- `epi_slide_{sum,mean,opt}` on ungrouped `epi_df`s will now temporarily group
32+
by `geo_value` and any `other_keys` for the slide operation rather than raise
33+
an error about duplicated time values. `epi_slide`'s analogous automatic
34+
grouping has been made temporary in order to match.
35+
- Improved speed of key-uniqueness checks.
36+
37+
## Bug fixes
38+
39+
- Removed `.window_size = 1` default from `epi_slide_{mean,sum,opt}`; this
40+
argument is now mandatory, and should nearly always be greater than 1 except
41+
for testing purposes.
42+
- Fixed `epi_slide_{sum,mean,opt}` raising an error on certain tidyselect
43+
expressions.
3244

3345
## Cleanup
3446

R/epi_df.R

+24-14
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ NULL
174174
#' @param other_keys If your tibble has additional keys, be sure to specify them
175175
#' as a character vector here (typical examples are "age" or sub-geographies).
176176
#' @param ... Additional arguments passed to methods.
177-
#' @return An `epi_df` object.
177+
#' @return * Of `new_epi_df()`: an `epi_df`
178178
#'
179179
#' @export
180180
new_epi_df <- function(x = tibble::tibble(geo_value = character(), time_value = as.Date(integer())),
@@ -205,6 +205,8 @@ new_epi_df <- function(x = tibble::tibble(geo_value = character(), time_value =
205205
#' to be converted
206206
#' @param ... used for specifying column names, as in [`dplyr::rename`]. For
207207
#' example, `geo_value = STATEFP, time_value = end_date`.
208+
#' @return * Of `as_epi_df()`: an (ungrouped) `epi_df`
209+
#'
208210
#' @export
209211
as_epi_df <- function(x, ...) {
210212
UseMethod("as_epi_df")
@@ -215,6 +217,7 @@ as_epi_df <- function(x, ...) {
215217
#' @method as_epi_df epi_df
216218
#' @export
217219
as_epi_df.epi_df <- function(x, ...) {
220+
x <- ungroup(x)
218221
return(x)
219222
}
220223

@@ -232,7 +235,6 @@ as_epi_df.tbl_df <- function(
232235
as_of,
233236
other_keys = character(),
234237
...) {
235-
# possible standard substitutions for time_value
236238
x <- rename(x, ...)
237239
x <- guess_column_name(x, "time_value", time_column_names())
238240
x <- guess_column_name(x, "geo_value", geo_column_names())
@@ -277,26 +279,32 @@ as_epi_df.tbl_df <- function(
277279
}
278280

279281
assert_character(other_keys)
282+
assert_subset(other_keys, names(x))
283+
# Fix up if given more than just other keys, at least until epipredict#428
284+
# merged:
285+
other_keys <- other_keys[!other_keys %in% c("geo_value", "time_value")]
280286

281287
if (".time_value_counts" %in% other_keys) {
282288
cli_abort("as_epi_df: `other_keys` can't include \".time_value_counts\"")
283289
}
284290

285-
duplicated_time_values <- x %>%
286-
group_by(across(all_of(c("geo_value", "time_value", other_keys)))) %>%
287-
filter(dplyr::n() > 1) %>%
288-
ungroup()
289-
if (nrow(duplicated_time_values) > 0) {
290-
bad_data <- capture.output(duplicated_time_values)
291-
cli_abort(
292-
"as_epi_df: some groups in the data have duplicated time values. epi_df requires a unique time_value per group.",
293-
body = c("Sample groups:", bad_data)
294-
)
295-
}
291+
assert(check_ukey_unique(x, c("geo_value", other_keys, "time_value"), c(
292+
">" = "If this is line list data, convert it to counts/rates first.",
293+
">" = "If this contains a demographic breakdown, check that you have
294+
specified appropriate `other_keys`" # . from checkmate
295+
)))
296296

297297
new_epi_df(x, geo_type, time_type, as_of, other_keys)
298298
}
299299

300+
#' @rdname epi_df
301+
#' @order 1
302+
#' @method as_epi_df grouped_df
303+
#' @export
304+
as_epi_df.grouped_df <- function(x, ...) {
305+
as_epi_df(ungroup(x), ...)
306+
}
307+
300308
#' @rdname epi_df
301309
#' @order 1
302310
#' @method as_epi_df data.frame
@@ -320,9 +328,11 @@ as_epi_df.tbl_ts <- function(x, as_of, other_keys = character(), ...) {
320328
#' Test for `epi_df` format
321329
#'
322330
#' @param x An object.
323-
#' @return `TRUE` if the object inherits from `epi_df`.
331+
#' @return * Of `is_epi_df`: `TRUE` if the object inherits from `epi_df`,
332+
#' otherwise `FALSE`.
324333
#'
325334
#' @rdname epi_df
335+
#' @order 1
326336
#' @export
327337
is_epi_df <- function(x) {
328338
inherits(x, "epi_df")

R/epiprocess-package.R

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
#' @import epidatasets
66
#' @importFrom checkmate anyInfinite anyMissing assert assert_character
77
#' @importFrom checkmate assert_class assert_data_frame assert_int assert_list
8+
#' @importFrom checkmate assert_false
89
#' @importFrom checkmate assert_logical assert_numeric assert_scalar checkInt
10+
#' @importFrom checkmate assert_string
11+
#' @importFrom checkmate assert_subset
12+
#' @importFrom checkmate assert_tibble
913
#' @importFrom checkmate check_atomic check_data_frame expect_class test_int
1014
#' @importFrom checkmate check_names
1115
#' @importFrom checkmate test_subset test_set_equal vname
1216
#' @importFrom cli cli_abort cli_warn
1317
#' @importFrom data.table as.data.table
1418
#' @importFrom data.table key
1519
#' @importFrom data.table setkeyv
20+
#' @importFrom dplyr arrange
21+
#' @importFrom dplyr is_grouped_df
1622
#' @importFrom dplyr select
1723
#' @importFrom lifecycle deprecated
1824
#' @importFrom rlang %||%
25+
#' @importFrom rlang is_bare_integerish
26+
#' @importFrom vctrs vec_data
27+
#' @importFrom vctrs vec_equal
1928
## usethis namespace: end
2029
NULL
2130

@@ -24,5 +33,5 @@ utils::globalVariables(c(
2433
"fitted", ".response", "geo_value", "time_value",
2534
"value", ".real", "lag", "max_value", "min_value",
2635
"median_value", "spread", "rel_spread", "time_to",
27-
"time_near_latest", "n_revisions"
36+
"time_near_latest", "n_revisions", "min_lag", "max_lag"
2837
))

R/methods-epi_archive.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,10 @@ epix_detailed_restricted_mutate <- function(.data, ...) {
688688
#' requested `.versions`) for rows having a `time_value` of at least `.version
689689
#' - before`. Otherwise, the slide computation will be passed only the most
690690
#' recent `version` for every unique `time_value`. Default is `FALSE`.
691-
#' @return A tibble whose columns are: the grouping variables, `time_value`,
692-
#' containing the reference time values for the slide computation, and a
693-
#' column named according to the `.new_col_name` argument, containing the slide
694-
#' values.
691+
#' @return A tibble whose columns are: the grouping variables (if any),
692+
#' `time_value`, containing the reference time values for the slide
693+
#' computation, and a column named according to the `.new_col_name` argument,
694+
#' containing the slide values. It will be grouped by the grouping variables.
695695
#'
696696
#' @details A few key distinctions between the current function and `epi_slide()`:
697697
#' 1. In `.f` functions for `epix_slide`, one should not assume that the input

R/reexports.R

+17-85
Original file line numberDiff line numberDiff line change
@@ -79,104 +79,36 @@ ggplot2::autoplot
7979

8080
# epidatasets -------------------------------------------------------------------
8181

82-
#' @inherit epidatasets::cases_deaths_subset description source references title
83-
#' @inheritSection epidatasets::cases_deaths_subset Data dictionary
84-
#' @examples
85-
#' # Since this is a re-exported dataset, it cannot be loaded using
86-
#' # the `data()` function. `data()` looks for a file of the same name
87-
#' # in the `data/` directory, which doesn't exist in this package.
88-
#' # works
89-
#' epiprocess::cases_deaths_subset
82+
#' @rdname epidatasets_reexports
9083
#'
91-
#' # works
92-
#' library(epiprocess)
93-
#' cases_deaths_subset
84+
#' @title Selected example data sets from `epidatasets`
9485
#'
95-
#' # fails
96-
#' \dontrun{
97-
#' data(cases_deaths_subset, package = "epiprocess")
98-
#' }
86+
#' @description Data sets re-exported from `epidatasets`; please see
87+
#' documentation for each of these objects in `epidatasets`.
88+
#'
89+
#' A brief description of the format of each of the objects above are described
90+
#' in matching order below.
91+
#'
92+
#' @keywords internal
9993
#' @export
10094
delayedAssign("cases_deaths_subset", epidatasets::cases_deaths_subset)
10195

102-
#' @inherit epidatasets::covid_incidence_county_subset description source references title
103-
#' @inheritSection epidatasets::covid_incidence_county_subset Data dictionary
104-
#' @examples
105-
#' # Since this is a re-exported dataset, it cannot be loaded using
106-
#' # the `data()` function. `data()` looks for a file of the same name
107-
#' # in the `data/` directory, which doesn't exist in this package.
108-
#' # works
109-
#' epiprocess::covid_incidence_county_subset
110-
#'
111-
#' # works
112-
#' library(epiprocess)
113-
#' covid_incidence_county_subset
114-
#'
115-
#' # fails
116-
#' \dontrun{
117-
#' data(covid_incidence_county_subset, package = "epiprocess")
118-
#' }
96+
#' @rdname epidatasets_reexports
97+
#' @keywords internal
11998
#' @export
12099
delayedAssign("covid_incidence_county_subset", epidatasets::covid_incidence_county_subset)
121100

122-
#' @inherit epidatasets::covid_incidence_outliers description source references title
123-
#' @inheritSection epidatasets::covid_incidence_outliers Data dictionary
124-
#' @examples
125-
#' # Since this is a re-exported dataset, it cannot be loaded using
126-
#' # the `data()` function. `data()` looks for a file of the same name
127-
#' # in the `data/` directory, which doesn't exist in this package.
128-
#' # works
129-
#' epiprocess::covid_incidence_outliers
130-
#'
131-
#' # works
132-
#' library(epiprocess)
133-
#' covid_incidence_outliers
134-
#'
135-
#' # fails
136-
#' \dontrun{
137-
#' data(covid_incidence_outliers, package = "epiprocess")
138-
#' }
101+
#' @rdname epidatasets_reexports
102+
#' @keywords internal
139103
#' @export
140104
delayedAssign("covid_incidence_outliers", epidatasets::covid_incidence_outliers)
141105

142-
#' @inherit epidatasets::archive_cases_dv_subset description source references title
143-
#' @inheritSection epidatasets::archive_cases_dv_subset Data dictionary
144-
#' @examples
145-
#' # Since this is a re-exported dataset, it cannot be loaded using
146-
#' # the `data()` function. `data()` looks for a file of the same name
147-
#' # in the `data/` directory, which doesn't exist in this package.
148-
#' # works
149-
#' epiprocess::archive_cases_dv_subset
150-
#'
151-
#' # works
152-
#' library(epiprocess)
153-
#' archive_cases_dv_subset
154-
#'
155-
#' # fails
156-
#' \dontrun{
157-
#' data(archive_cases_dv_subset, package = "epiprocess")
158-
#' }
159-
#'
106+
#' @rdname epidatasets_reexports
107+
#' @keywords internal
160108
#' @export
161109
delayedAssign("archive_cases_dv_subset", epidatasets::archive_cases_dv_subset)
162110

163-
#' @inherit epidatasets::covid_case_death_rates_extended description source references title
164-
#' @inheritSection epidatasets::covid_case_death_rates_extended Data dictionary
165-
#' @examples
166-
#' # Since this is a re-exported dataset, it cannot be loaded using
167-
#' # the `data()` function. `data()` looks for a file of the same name
168-
#' # in the `data/` directory, which doesn't exist in this package.
169-
#' # works
170-
#' epiprocess::covid_case_death_rates_extended
171-
#'
172-
#' # works
173-
#' library(epiprocess)
174-
#' covid_case_death_rates_extended
175-
#'
176-
#' # fails
177-
#' \dontrun{
178-
#' data(covid_case_death_rates_extended, package = "epiprocess")
179-
#' }
180-
#'
111+
#' @rdname epidatasets_reexports
112+
#' @keywords internal
181113
#' @export
182114
delayedAssign("covid_case_death_rates_extended", epidatasets::covid_case_death_rates_extended)

0 commit comments

Comments
 (0)