Debian Codegraph @

R/sfl.R

raw
1 #' Create a skimr function list
2 #'
3 #' This constructor is used to create a named list of functions. It also you
4 #' also pass `NULL` to identify a skimming function that you wish to remove.
5 #' Only functions that return a single value, working with [dplyr::summarize()],
6 #' can be used within `sfl`.
7 #'
8 #' `sfl()` will automatically generate callables and names for a variety of
9 #' inputs, including functions, formulas and strings. Nonetheless, we recommend
10 #' providing names when reasonable to get better [skim()] output.
11 #'
12 #' @param ... Inherited from dplyr::data_masking() for dplyr version 1 or later
13 #' or dplyr::funs() for older versions of dplyr.
14 #' A list of functions
15 #' specified by:
16 #'
17 #' * Their name, `"mean"`
18 #' * The function itself, `mean`
19 #' * A call to the function with `.` as a dummy argument,
20 #' `mean(., na.rm = TRUE)`
21 #' * An anonymous function in \pkg{purrr} notation, `~mean(., na.rm = TRUE)`
22 #'
23 #' @param skim_type A character scalar. This is used to match locally-provided
24 #' skimmers with defaults. See [get_skimmers()] for more detail.
25 #' @return A `skimr_function_list`, which contains a list of `fun_calls`,
26 #' returned by `dplyr::funs()` and a list of skimming functions to drop.
27 #' @seealso [dplyr::funs()], [skim_with()] and [get_skimmers()].
28 #' @examples
29 #' # sfl's can take a variety of input formats and will generate names
30 #' # if not provided.
31 #' sfl(mad, "var", ~ length(.)^2)
32 #'
33 #' # But these can generate unpredictable names in your output.
34 #' # Better to set your own names.
35 #' sfl(mad = mad, variance = "var", length_sq = ~ length(.)^2)
36 #'
37 #' # sfl's can remove individual skimmers from defaults by passing NULL.
38 #' sfl(hist = NULL)
39 #'
40 #' # When working interactively, you don't need to set a type.
41 #' # But you should when defining new defaults with `get_skimmers()`.
42 #' get_skimmers.my_new_class <- function(column) {
43 #' sfl(n_missing, skim_type = "my_new_class")
44 #' }
45 #' @export
46 sfl <- function(..., skim_type = "") {
47 stopifnot(length(skim_type) == 1, is.character(skim_type))
48 funs <- build_sfl_names(...)
49 structure(
50 list(funs = funs, skim_type = skim_type),
51 class = "skimr_function_list"
52 )
53 }
54
55 build_sfl_names <- function(...) {
56 labels <- rlang::quos_auto_name(rlang::enquos(...))
57 rlang::set_names(rlang::list2(...), names(labels))
58 }