|
| 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