| 1 |
#' @include skim_with.R stats.R |
| 2 |
NULL |
| 3 |
|
| 4 |
#' Retrieve the summary functions for a specific data type |
| 5 |
#' |
| 6 |
#' These functions are used to set the default skimming functions for a data |
| 7 |
#' type. They are combined with the base skim function list (`sfl`) in |
| 8 |
#' [skim_with()], to create the summary tibble for each type. |
| 9 |
#' |
| 10 |
#' When creating your own set of skimming functions, call [sfl()] within a |
| 11 |
#' [get_skimmers()] method for your particular type. Your call to [sfl()] should |
| 12 |
#' also provide a matching class in the `skim_type` argument. Otherwise, it |
| 13 |
#' will not be possible to dynamically reassign your default functions when |
| 14 |
#' working interactively. |
| 15 |
#' |
| 16 |
#' Call [get_default_skimmers()] to see the functions for each type of summary |
| 17 |
#' function currently supported. Call [get_default_skimmer_names()] to just see |
| 18 |
#' the names of these functions. Use [modify_default_skimmers()] for a method |
| 19 |
#' for changing the `skim_type` or functions for a default `sfl`. This is useful |
| 20 |
#' for creating new default `sfl`'s. |
| 21 |
#' |
| 22 |
#' @param column An atomic vector or list. A column from a data frame. |
| 23 |
#' @param skim_type A character scalar. The class of the object with default |
| 24 |
#' skimmers. |
| 25 |
#' @return A `skim_function_list` object. |
| 26 |
#' @seealso [sfl()] |
| 27 |
#' @examples |
| 28 |
#' # Defining default skimming functions for a new class, `my_class`. |
| 29 |
#' # Note that the class argument is required for dynamic reassignment. |
| 30 |
#' get_skimmers.my_class <- function(column) { |
| 31 |
#' sfl( |
| 32 |
#' skim_type = "my_class", |
| 33 |
#' mean, |
| 34 |
#' sd |
| 35 |
#' ) |
| 36 |
#' } |
| 37 |
#' |
| 38 |
#' # Integer and double columns are both "numeric" and are treated the same |
| 39 |
#' # by default. To switch this behavior in another package, add a method. |
| 40 |
#' get_skimmers.integer <- function(column) { |
| 41 |
#' sfl( |
| 42 |
#' skim_type = "integer", |
| 43 |
#' p50 = ~ stats::quantile( |
| 44 |
#' ., |
| 45 |
#' probs = .50, na.rm = TRUE, names = FALSE, type = 1 |
| 46 |
#' ) |
| 47 |
#' ) |
| 48 |
#' } |
| 49 |
#' x <- mtcars[c("gear", "carb")] |
| 50 |
#' class(x$carb) <- "integer" |
| 51 |
#' skim(x) |
| 52 |
#' \dontrun{ |
| 53 |
#' # In a package, to revert to the V1 behavior of skimming separately with the |
| 54 |
#' # same functions, assign the numeric `get_skimmers`. |
| 55 |
#' get_skimmers.integer <- skimr::get_skimmers.numeric |
| 56 |
#' |
| 57 |
#' # Or, in a local session, use `skim_with` to create a different `skim`. |
| 58 |
#' new_skim <- skim_with(integer = skimr::get_skimmers.numeric()) |
| 59 |
#' |
| 60 |
#' # To apply a set of skimmers from an old type to a new type |
| 61 |
#' get_skimmers.new_type <- function(column) { |
| 62 |
#' modify_default_skimmers("old_type", new_skim_type = "new_type") |
| 63 |
#' } |
| 64 |
#' } |
| 65 |
#' @export |
| 66 |
get_skimmers <- function(column) { |
| 67 |
UseMethod("get_skimmers") |
| 68 |
} |
| 69 |
|
| 70 |
#' @describeIn get_skimmers The default method for skimming data. Only used when |
| 71 |
#' a column's data type doesn't match currently installed types. Call |
| 72 |
#' [get_default_skimmer_names] to see these defaults. |
| 73 |
#' @export |
| 74 |
get_skimmers.default <- function(column) { |
| 75 |
modify_default_skimmers("character", new_skim_type = "default") |
| 76 |
} |
| 77 |
|
| 78 |
#' @describeIn get_skimmers Summary functions for numeric columns, covering both |
| 79 |
#' [double()] and [integer()] classes: [mean()], [sd()], [quantile()] and |
| 80 |
#' [inline_hist()]. |
| 81 |
#' @export |
| 82 |
get_skimmers.numeric <- function(column) { |
| 83 |
sfl( |
| 84 |
skim_type = "numeric", |
| 85 |
mean = ~ mean(., na.rm = TRUE), |
| 86 |
sd = ~ stats::sd(., na.rm = TRUE), |
| 87 |
p0 = ~ stats::quantile(., probs = 0, na.rm = TRUE, names = FALSE), |
| 88 |
p25 = ~ stats::quantile(., probs = .25, na.rm = TRUE, names = FALSE), |
| 89 |
p50 = ~ stats::quantile(., probs = .50, na.rm = TRUE, names = FALSE), |
| 90 |
p75 = ~ stats::quantile(., probs = .75, na.rm = TRUE, names = FALSE), |
| 91 |
p100 = ~ stats::quantile(., probs = 1, na.rm = TRUE, names = FALSE), |
| 92 |
hist = ~ inline_hist(., 5) |
| 93 |
) |
| 94 |
} |
| 95 |
|
| 96 |
#' @describeIn get_skimmers Summary functions for factor columns: |
| 97 |
#' [is.ordered()], [n_unique()] and [top_counts()]. |
| 98 |
#' @export |
| 99 |
get_skimmers.factor <- function(column) { |
| 100 |
sfl( |
| 101 |
skim_type = "factor", |
| 102 |
ordered = is.ordered, |
| 103 |
n_unique = n_unique, |
| 104 |
top_counts = top_counts |
| 105 |
) |
| 106 |
} |
| 107 |
|
| 108 |
#' @describeIn get_skimmers Summary functions for character columns. Also, the |
| 109 |
#' default for unknown columns: [min_char()], [max_char()], [n_empty()], |
| 110 |
#' [n_unique()] and [n_whitespace()]. |
| 111 |
#' @export |
| 112 |
get_skimmers.character <- function(column) { |
| 113 |
sfl( |
| 114 |
skim_type = "character", |
| 115 |
min = min_char, |
| 116 |
max = max_char, |
| 117 |
empty = n_empty, |
| 118 |
n_unique = n_unique, |
| 119 |
whitespace = n_whitespace |
| 120 |
) |
| 121 |
} |
| 122 |
|
| 123 |
#' @describeIn get_skimmers Summary functions for logical/ boolean columns: |
| 124 |
#' [mean()], which produces rates for each value, and [top_counts()]. |
| 125 |
#' @export |
| 126 |
get_skimmers.logical <- function(column) { |
| 127 |
sfl( |
| 128 |
skim_type = "logical", |
| 129 |
mean = ~ mean(., na.rm = TRUE), |
| 130 |
count = top_counts |
| 131 |
) |
| 132 |
} |
| 133 |
|
| 134 |
#' @describeIn get_skimmers Summary functions for complex columns: [mean()]. |
| 135 |
#' @export |
| 136 |
get_skimmers.complex <- function(column) { |
| 137 |
sfl( |
| 138 |
skim_type = "complex", |
| 139 |
mean = ~ mean(., na.rm = TRUE) |
| 140 |
) |
| 141 |
} |
| 142 |
|
| 143 |
#' @describeIn get_skimmers Summary functions for `Date` columns: [min()], |
| 144 |
#' [max()], [median()] and [n_unique()]. |
| 145 |
#' @export |
| 146 |
get_skimmers.Date <- function(column) { |
| 147 |
sfl( |
| 148 |
skim_type = "Date", |
| 149 |
min = min_date, |
| 150 |
max = max_date, |
| 151 |
median = ~ stats::median(., na.rm = TRUE), |
| 152 |
n_unique = n_unique |
| 153 |
) |
| 154 |
} |
| 155 |
|
| 156 |
#' @describeIn get_skimmers Summary functions for `POSIXct` columns: [min()], |
| 157 |
#' [max()], [median()] and [n_unique()]. |
| 158 |
#' @export |
| 159 |
get_skimmers.POSIXct <- function(column) { |
| 160 |
modify_default_skimmers("Date", new_skim_type = "POSIXct") |
| 161 |
} |
| 162 |
|
| 163 |
#' @describeIn get_skimmers Summary functions for `difftime` columns: [min()], |
| 164 |
#' [max()], [median()] and [n_unique()]. |
| 165 |
#' @export |
| 166 |
get_skimmers.difftime <- function(column) { |
| 167 |
modify_default_skimmers("Date", new_skim_type = "difftime") |
| 168 |
} |
| 169 |
|
| 170 |
#' @describeIn get_skimmers Summary functions for `Timespan` columns: [min()], |
| 171 |
#' [max()], [median()] and [n_unique()]. |
| 172 |
#' @export |
| 173 |
get_skimmers.Timespan <- function(column) { |
| 174 |
modify_default_skimmers("difftime", new_skim_type = "Timespan") |
| 175 |
} |
| 176 |
|
| 177 |
#' @describeIn get_skimmers Summary functions for `ts` columns: [min()], |
| 178 |
#' [max()], [median()] and [n_unique()]. |
| 179 |
#' @export |
| 180 |
get_skimmers.ts <- function(column) { |
| 181 |
sfl( |
| 182 |
skim_type = "ts", |
| 183 |
start = ts_start, |
| 184 |
end = ts_end, |
| 185 |
frequency = stats::frequency, |
| 186 |
deltat = stats::deltat, |
| 187 |
mean = ~ mean(., na.rm = TRUE), |
| 188 |
sd = ~ stats::sd(., na.rm = TRUE), |
| 189 |
min = ~ min(., na.rm = TRUE), |
| 190 |
max = ~ max(., na.rm = TRUE), |
| 191 |
median = ~ stats::median(., na.rm = TRUE), |
| 192 |
line_graph = ~ inline_linegraph(., 16) |
| 193 |
) |
| 194 |
} |
| 195 |
|
| 196 |
#' @describeIn get_skimmers Summary functions for `list` columns: [n_unique()], |
| 197 |
#' [list_min_length()] and [list_max_length()]. |
| 198 |
#' @export |
| 199 |
get_skimmers.list <- function(column) { |
| 200 |
sfl( |
| 201 |
skim_type = "list", |
| 202 |
n_unique = n_unique, |
| 203 |
min_length = list_min_length, |
| 204 |
max_length = list_max_length |
| 205 |
) |
| 206 |
} |
| 207 |
|
| 208 |
#' @describeIn get_skimmers Summary functions for `AsIs` columns: [n_unique()], |
| 209 |
#' [list_min_length()] and [list_max_length()]. |
| 210 |
#' @export |
| 211 |
get_skimmers.AsIs <- function(column) { |
| 212 |
modify_default_skimmers("list", new_skim_type = "AsIs") |
| 213 |
} |
| 214 |
|
| 215 |
#' @describeIn get_skimmers Summary functions for `haven_labelled` columns. |
| 216 |
#' Finds the appropriate skimmers for the underlying data in the vector. |
| 217 |
#' @export |
| 218 |
get_skimmers.haven_labelled <- function(column) { |
| 219 |
if (!requireNamespace("haven", quietly = TRUE)){ |
| 220 |
return() |
| 221 |
} |
| 222 |
get_skimmers(vctrs::vec_data(column)) |
| 223 |
} |
| 224 |
|
| 225 |
#' @rdname get_skimmers |
| 226 |
#' @param new_skim_type The type to assign to the looked up set of skimmers. |
| 227 |
#' @param new_funs Replacement functions for those in |
| 228 |
#' @export |
| 229 |
modify_default_skimmers <- function(skim_type, |
| 230 |
new_skim_type = NULL, |
| 231 |
new_funs = list()) { |
| 232 |
funs <- get_one_default_skimmer(skim_type) |
| 233 |
new_funs <- purrr::list_modify(funs, !!!new_funs) |
| 234 |
sfl(skim_type = new_skim_type %||% skim_type, !!!new_funs) |
| 235 |
} |
| 236 |
|
| 237 |
#' View default skimmer names and functions |
| 238 |
#' |
| 239 |
#' These utility functions look up the currently-available defaults for one or |
| 240 |
#' more `skim_type`'s. They work with all defaults in the `skimr` package, as |
| 241 |
#' well as the defaults in any package that extends `skimr`. See |
| 242 |
#' [get_skimmers()] for writing lookup methods for different. |
| 243 |
#' |
| 244 |
#' The functions differ in return type and whether or not the result is in |
| 245 |
#' a list. [get_default_skimmers()] and [get_one_default_skimmer()] return |
| 246 |
#' functions. The former returns functions within a typed list, i.e. |
| 247 |
#' `list(numeric = list(...functions...))`. |
| 248 |
#' |
| 249 |
#' The functions differ in return type and whether or not the result is in |
| 250 |
#' a list. [get_default_skimmer_names()] and [get_one_default_skimmer_names()] |
| 251 |
#' return a list of character vectors or a single character vector. |
| 252 |
#' |
| 253 |
#' [get_sfl()] returns the skimmer function list for a particular `skim_type`. |
| 254 |
#' It differs from [get_default_skimmers()] in that the returned `sfl` contains |
| 255 |
#' a list of functions and a `skim_type`. |
| 256 |
#' |
| 257 |
#' @param skim_type The class of the column being skimmed. |
| 258 |
#' @export |
| 259 |
get_default_skimmers <- function(skim_type = NULL) { |
| 260 |
if (is.null(skim_type)) { |
| 261 |
defaults <- as.character(utils::methods("get_skimmers")) |
| 262 |
types <- stringr::str_replace(defaults, "get_skimmers.", "") |
| 263 |
no_default <- purrr::discard(types, ~ .x == "default") |
| 264 |
iter <- rlang::set_names(no_default) |
| 265 |
} else { |
| 266 |
iter <- rlang::set_names(skim_type) |
| 267 |
} |
| 268 |
|
| 269 |
results <- purrr::map(iter, get_one_default_skimmer) |
| 270 |
no_defaults_for_class <- purrr::map_lgl(results, anyNA) |
| 271 |
results[!no_defaults_for_class] |
| 272 |
} |
| 273 |
|
| 274 |
#' @describeIn get_default_skimmers Get the functions associated with one |
| 275 |
#' `skim_type`. |
| 276 |
#' @export |
| 277 |
get_one_default_skimmer <- function(skim_type) { |
| 278 |
skimmers <- get_sfl(skim_type) |
| 279 |
skimmers$funs |
| 280 |
} |
| 281 |
|
| 282 |
#' @describeIn get_default_skimmers Get the names of the functions used in one |
| 283 |
#' or more `skim_type`'s. |
| 284 |
#' @export |
| 285 |
get_default_skimmer_names <- function(skim_type = NULL) { |
| 286 |
if (!is.null(skim_type) && "haven_labelled" %in% skim_type && |
| 287 |
!requireNamespace("haven", quietly = TRUE)) { |
| 288 |
return() |
| 289 |
} |
| 290 |
skimmers <- get_default_skimmers(skim_type) |
| 291 |
purrr::map(skimmers, names) |
| 292 |
} |
| 293 |
|
| 294 |
#' @describeIn get_default_skimmers Get the names of the functions used in one |
| 295 |
#' `skim_type`. |
| 296 |
#' @export |
| 297 |
get_one_default_skimmer_names <- function(skim_type) { |
| 298 |
skimmers <- get_one_default_skimmer(skim_type) |
| 299 |
names(skimmers) |
| 300 |
} |
| 301 |
|
| 302 |
#' @describeIn get_default_skimmers Get the `sfl` for a `skim_type`. |
| 303 |
#' @export |
| 304 |
get_sfl <- function(skim_type) { |
| 305 |
skimmers <- get_skimmers(structure(integer(), class = skim_type)) |
| 306 |
if (!is.null(skim_type) & skim_type == "haven_labelled" & |
| 307 |
!requireNamespace("haven", quietly = TRUE)) { |
| 308 |
return() |
| 309 |
} |
| 310 |
if (skimmers$skim_type == "default") { |
| 311 |
warning("There are no default skimmers for type: ", skim_type) |
| 312 |
sfl(NA, skim_type = NA_character_) |
| 313 |
} else { |
| 314 |
skimmers |
| 315 |
} |
| 316 |
} |