Skip to content

Commit e1d300d

Browse files
committed
feat(epi_slide): catch common operations x forgotten colname
See #475. We have the ability to catch this consistently now since we are using `.keep = TRUE`, so the .x into each slide computation is an `epi_df` even if we're grouping by `geo_value` (previously, .x would have decayed into a tibble and we shouldn't override tibble's behavior). Also add missing `group_map` import.
1 parent 53005ff commit e1d300d

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Collate:
7979
'correlation.R'
8080
'data.R'
8181
'epi_df.R'
82+
'epi_df_forbidden_methods.R'
8283
'epiprocess.R'
8384
'group_by_epi_df_methods.R'
8485
'methods-epi_archive.R'

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
S3method("[",epi_df)
44
S3method("names<-",epi_df)
5+
S3method(Summary,epi_df)
56
S3method(arrange_canonical,default)
67
S3method(arrange_canonical,epi_df)
78
S3method(as_epi_df,data.frame)
@@ -36,6 +37,7 @@ S3method(key_colnames,data.frame)
3637
S3method(key_colnames,default)
3738
S3method(key_colnames,epi_archive)
3839
S3method(key_colnames,epi_df)
40+
S3method(mean,epi_df)
3941
S3method(next_after,Date)
4042
S3method(next_after,integer)
4143
S3method(print,epi_archive)
@@ -148,6 +150,7 @@ importFrom(dplyr,everything)
148150
importFrom(dplyr,filter)
149151
importFrom(dplyr,group_by)
150152
importFrom(dplyr,group_by_drop_default)
153+
importFrom(dplyr,group_map)
151154
importFrom(dplyr,group_modify)
152155
importFrom(dplyr,group_vars)
153156
importFrom(dplyr,groups)

R/epi_df_forbidden_methods.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Methods in this file are used to
2+
# * Disable problematic inherited behavior (e.g., mean on epi_dfs)
3+
# * Provide better error messaging if possible for things that already abort
4+
# when they should (e.g., sum on epi_dfs)
5+
6+
7+
# Disable mean on epi_dfs, to prevent `epi_slide(~ mean(.x), ....)` bad output:
8+
9+
#' @export
10+
mean.epi_df <- function(x, ...) {
11+
cli_abort(c(
12+
"`mean` shouldn't be used on entire `epi_df`s",
13+
"x" = "{rlang::caller_arg(x)} was an `epi_df`",
14+
"i" = "If you encountered this while trying to take a rolling mean
15+
of a column using `epi_slide`, you probably forgot to
16+
specify the column name (e.g., ~ mean(.x$colname)). You may
17+
also prefer to use the specialized `epi_slide_mean` method."
18+
))
19+
}
20+
21+
# Similarly, provide better error messages for some other potentially-common
22+
# slide operations (sum, prod, min, max, all, any, range):
23+
24+
#' @export
25+
Summary.epi_df <- function(..., na.rm = FALSE) {
26+
# cli uses dot prefixes for special purpose; use alias to avoid confusion during interpolation
27+
generic <- .Generic
28+
opt_pointer <- switch(.Generic,
29+
sum = "You may also prefer to use the specialized `epi_slide_sum` method.",
30+
prod = ,
31+
min = ,
32+
max = ,
33+
all = ,
34+
any = "You may also prefer to use the specialized `epi_slide_opt` method.",
35+
range = "",
36+
cli_abort("Unrecognized .Generic: {generic}")
37+
)
38+
cli_abort(c(
39+
"`{generic}` shouldn't be used on entire `epi_df`s",
40+
# We'd like to quote user input in the error message, but `caller_arg(..1)` is
41+
# just "..1" and (eagerness/S4/unnamedness?) issues thwart some alternatives; just
42+
# use something generic:
43+
"x" = "`{generic}`'s first argument was an `epi_df`",
44+
"i" = "If you encountered this while trying to take a rolling {generic}
45+
of a column using `epi_slide`, you probably forgot to
46+
specify the column name (e.g., ~ mean(.x$colname)). {opt_pointer}"
47+
))
48+
}

R/slide.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#' @template basic-slide-details
3737
#'
3838
#' @importFrom lubridate days weeks
39-
#' @importFrom dplyr bind_rows group_vars filter select
39+
#' @importFrom dplyr bind_rows group_map group_vars filter select
4040
#' @importFrom rlang .data .env !! enquos sym env missing_arg
4141
#' @export
4242
#' @seealso [`epi_slide_opt`] [`epi_slide_mean`] [`epi_slide_sum`]

0 commit comments

Comments
 (0)