Skip to content

Commit 97729a5

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).
1 parent 53005ff commit 97729a5

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
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: 2 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)

R/epi_df_forbidden_methods.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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("`mean` can't be used on entire `epi_df`s",
12+
"x" = "{rlang::caller_arg(x)} was an `epi_df`",
13+
"i" = "If you encountered this while trying to take a rolling mean
14+
of a column using `epi_slide`, you probably forgot to
15+
specify the column name (e.g., ~ mean(.x$colname)). You may
16+
also prefer to use the specialized `epi_slide_mean` method."))
17+
}
18+
19+
# Similarly, provide better error messages for some other potentially-common
20+
# slide operations (sum, prod, min, max, all, any, range):
21+
22+
#' @export
23+
Summary.epi_df <- function(..., na.rm = FALSE) {
24+
generic <- .Generic # cli uses dot prefixes for special purpose, don't upset it
25+
maybe_opt_pointer <- switch(
26+
.Generic,
27+
sum = "You may also prefer the specialized epi_slide_sum method.",
28+
prod =, min =, max =, all =, any = "You may also prefer the specialized epi_slide_opt method.",
29+
range = "",
30+
cli_abort("Unrecognized .Generic: {generic}")
31+
)
32+
cli_abort(c("`{generic}` can't be used on entire `epi_df`s",
33+
"x" = "{rlang::caller_arg(..1)} was an `epi_df`",
34+
"i" = "If you encountered this while trying to take a rolling {generic}
35+
of a column using `epi_slide`, you probably forgot to
36+
specify the column name (e.g., ~ mean(.x$colname)).
37+
{maybe_opt_pointer}"))
38+
}

0 commit comments

Comments
 (0)