|
| 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 | +} |
0 commit comments