| 1 |
#' Set or add the summary functions for a particular type of data |
| 2 |
#' |
| 3 |
#' While skim is designed around having an opinionated set of defaults, you |
| 4 |
#' can use this function to change the summary statistics that it returns. |
| 5 |
#' |
| 6 |
#' `skim_with()` is a closure: a function that returns a new function. This |
| 7 |
#' lets you have several skimming functions in a single R session, but it |
| 8 |
#' also means that you need to assign the return of `skim_with()` before |
| 9 |
#' you can use it. |
| 10 |
#' |
| 11 |
#' You assign values within `skim_with` by using the [sfl()] helper (`skimr` |
| 12 |
#' function list). This helper behaves mostly like [dplyr::funs()], but lets |
| 13 |
#' you also identify which skimming functions you want to remove, by setting |
| 14 |
#' them to `NULL`. Assign an `sfl` to each column type that you wish to modify. |
| 15 |
#' |
| 16 |
#' Functions that summarize all data types, and always return the same type |
| 17 |
#' of value, can be assigned to the `base` argument. The default base skimmers |
| 18 |
#' compute the number of missing values [n_missing()] and the rate of values |
| 19 |
#' being complete, i.e. not missing, [complete_rate()]. |
| 20 |
#' |
| 21 |
#' When `append = TRUE` and local skimmers have names matching the names of |
| 22 |
#' entries in the default `skim_function_list`, the values in the default list |
| 23 |
#' are overwritten. Similarly, if `NULL` values are passed within `sfl()`, these |
| 24 |
#' default skimmers are dropped. Otherwise, if `append = FALSE`, only the |
| 25 |
#' locally-provided skimming functions are used. |
| 26 |
#' |
| 27 |
#' Note that `append` only applies to the `typed` skimmers (i.e. non-base). |
| 28 |
#' See [get_default_skimmer_names()] for a list of defaults. |
| 29 |
#' |
| 30 |
#' @param ... One or more (`sfl`) `skimmer_function_list` objects, with an |
| 31 |
#' argument name that matches a particular data type. |
| 32 |
#' @param base An `sfl` that sets skimmers for all column types. |
| 33 |
#' @param append Whether the provided options should be in addition to the |
| 34 |
#' defaults already in `skim`. Default is `TRUE`. |
| 35 |
#' @return A new `skim()` function. This is callable. See [skim()] for more |
| 36 |
#' details. |
| 37 |
#' @examples |
| 38 |
#' # Use new functions for numeric functions. If you don't provide a name, |
| 39 |
#' # one will be automatically generated. |
| 40 |
#' my_skim <- skim_with(numeric = sfl(median, mad), append = FALSE) |
| 41 |
#' my_skim(faithful) |
| 42 |
#' |
| 43 |
#' # If you want to remove a particular skimmer, set it to NULL |
| 44 |
#' # This removes the inline histogram |
| 45 |
#' my_skim <- skim_with(numeric = sfl(hist = NULL)) |
| 46 |
#' my_skim(faithful) |
| 47 |
#' |
| 48 |
#' # This works with multiple skimmers. Just match names to overwrite |
| 49 |
#' my_skim <- skim_with(numeric = sfl(iqr = IQR, p25 = NULL, p75 = NULL)) |
| 50 |
#' my_skim(faithful) |
| 51 |
#' |
| 52 |
#' # Alternatively, set `append = FALSE` to replace the skimmers of a type. |
| 53 |
#' my_skim <- skim_with(numeric = sfl(mean = mean, sd = sd), append = FALSE) |
| 54 |
#' |
| 55 |
#' # Skimmers are unary functions. Partially apply arguments during assigment. |
| 56 |
#' # For example, you might want to remove NA values. |
| 57 |
#' my_skim <- skim_with(numeric = sfl(iqr = ~ IQR(., na.rm = TRUE))) |
| 58 |
#' |
| 59 |
#' # Set multiple types of skimmers simultaneously. |
| 60 |
#' my_skim <- skim_with(numeric = sfl(mean), character = sfl(length)) |
| 61 |
#' |
| 62 |
#' # Or pass the same as a list, unquoting the input. |
| 63 |
#' my_skimmers <- list(numeric = sfl(mean), character = sfl(length)) |
| 64 |
#' my_skim <- skim_with(!!!my_skimmers) |
| 65 |
#' |
| 66 |
#' # Use the v1 base skimmers instead. |
| 67 |
#' my_skim <- skim_with(base = sfl( |
| 68 |
#' missing = n_missing, |
| 69 |
#' complete = n_complete, |
| 70 |
#' n = length |
| 71 |
#' )) |
| 72 |
#' |
| 73 |
#' # Remove the base skimmers entirely |
| 74 |
#' my_skim <- skim_with(base = NULL) |
| 75 |
#' @importFrom rlang .data |
| 76 |
#' @export |
| 77 |
skim_with <- function(..., |
| 78 |
base = sfl( |
| 79 |
n_missing = n_missing, |
| 80 |
complete_rate = complete_rate |
| 81 |
), |
| 82 |
append = TRUE) { |
| 83 |
stopifnot(is.null(base) || inherits(base, "skimr_function_list")) |
| 84 |
local_skimmers <- validate_assignment(...) |
| 85 |
|
| 86 |
function(data, ..., .data_name = NULL) { |
| 87 |
if (is.null(.data_name)) { |
| 88 |
.data_name <- rlang::expr_label(substitute(data)) |
| 89 |
} |
| 90 |
|
| 91 |
if (!inherits(data, "data.frame")) { |
| 92 |
data <- as.data.frame(data) |
| 93 |
} |
| 94 |
stopifnot(inherits(data, "data.frame")) |
| 95 |
|
| 96 |
selected <- names(tidyselect::eval_select(rlang::expr(c(...)), data)) |
| 97 |
if (length(selected) == 0) { |
| 98 |
selected <- names(data) |
| 99 |
} |
| 100 |
|
| 101 |
grps <- dplyr::groups(data) |
| 102 |
if (length(grps) > 0) { |
| 103 |
group_variables <- selected %in% as.character(grps) |
| 104 |
selected <- selected[!group_variables] |
| 105 |
} else { |
| 106 |
attr(data, "groups") <- list() |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
skimmers <- purrr::map( |
| 111 |
selected, get_final_skimmers, data, local_skimmers, append |
| 112 |
) |
| 113 |
types <- purrr::map_chr(skimmers, "skim_type") |
| 114 |
unique_skimmers <- reduce_skimmers(skimmers, types) |
| 115 |
combined_skimmers <- purrr::map(unique_skimmers, join_with_base, base) |
| 116 |
ready_to_skim <- tibble::tibble( |
| 117 |
skim_type = unique(types), |
| 118 |
skimmers = purrr::map(combined_skimmers, mangle_names, names(base$funs)), |
| 119 |
skim_variable = split(selected, types)[unique(types)] |
| 120 |
) |
| 121 |
|
| 122 |
grouped <- dplyr::group_by(ready_to_skim, .data$skim_type) |
| 123 |
nested <- dplyr::summarize( |
| 124 |
grouped, |
| 125 |
skimmed = purrr::map2( |
| 126 |
.data$skimmers, .data$skim_variable, skim_by_type, data |
| 127 |
) |
| 128 |
) |
| 129 |
structure( |
| 130 |
tidyr::unnest(nested, "skimmed"), |
| 131 |
class = c("skim_df", "tbl_df", "tbl", "data.frame"), |
| 132 |
data_rows = nrow(data), |
| 133 |
data_cols = ncol(data), |
| 134 |
df_name = .data_name, |
| 135 |
dt_key = get_dt_key(data), |
| 136 |
groups = dplyr::group_vars(data), |
| 137 |
base_skimmers = names(base$funs), |
| 138 |
skimmers_used = get_skimmers_used(unique_skimmers) |
| 139 |
) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#' Process arguments provided in `skim_with` |
| 144 |
#' |
| 145 |
#' Make sure that arguments provided in `skim_with()` have names. Also, |
| 146 |
#' check if we are defining a new skimming type dynamically. |
| 147 |
#' |
| 148 |
#' @keywords internal |
| 149 |
#' @noRd |
| 150 |
validate_assignment <- function(...) { |
| 151 |
to_assign <- rlang::list2(...) |
| 152 |
|
| 153 |
if (length(to_assign) < 1) { |
| 154 |
return(to_assign) |
| 155 |
} |
| 156 |
|
| 157 |
# Need to cope with case where ... is a list already |
| 158 |
if (!inherits(to_assign[[1]], "skimr_function_list")) { |
| 159 |
to_assign <- to_assign[[1]] |
| 160 |
} |
| 161 |
|
| 162 |
proposed_names <- names(to_assign) |
| 163 |
if (!all(nzchar(proposed_names)) || is.null(proposed_names) || |
| 164 |
anyNA(proposed_names)) { |
| 165 |
stop("skim_with requires all arguments to be named.", call. = FALSE) |
| 166 |
} |
| 167 |
|
| 168 |
defaults <- get_default_skimmers() |
| 169 |
existing <- proposed_names %in% names(defaults) |
| 170 |
|
| 171 |
if (!all(existing) && length(defaults) > 0) { |
| 172 |
collapsed <- paste(proposed_names[!existing], collapse = ", ") |
| 173 |
message( |
| 174 |
"Creating new skimming functions for the following classes: ", |
| 175 |
collapsed, ".\nThey did not have recognized defaults. Call ", |
| 176 |
"get_default_skimmers() for more information." |
| 177 |
) |
| 178 |
} |
| 179 |
to_assign |
| 180 |
} |
| 181 |
|
| 182 |
#' Sets the appropriate key value when working with `data.table` |
| 183 |
#' @keywords internal |
| 184 |
#' @noRd |
| 185 |
get_dt_key <- function(data) { |
| 186 |
if (inherits(data, "data.table")) { |
| 187 |
dt_key <- data.table::key(data) |
| 188 |
if (is.null(dt_key)) { |
| 189 |
dt_key <- "NULL" |
| 190 |
} |
| 191 |
paste(dt_key, collapse = ", ") |
| 192 |
} else { |
| 193 |
NA # Will never be NA if `data` is a data.table |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
#' Combine local and default skimmers for each column |
| 198 |
#' |
| 199 |
#' Get the default skimmers for the current column using S3 dispatch for |
| 200 |
#' [get_skimmers()]. Get the user-provided local skimmers from [skim_with()]. |
| 201 |
#' If no local skimmers are provided, use the defaults. Otherwise, merge the |
| 202 |
#' local and default skimmers with the following rules. |
| 203 |
#' |
| 204 |
#' - If `append = FALSE` of if the local and default types differ, use only |
| 205 |
#' the locals. |
| 206 |
#' - Else, replace the default values with the local values. |
| 207 |
#' |
| 208 |
#' @param column A character scalar. The column name. |
| 209 |
#' @param data The data frame to summarize. |
| 210 |
#' @param local_skimmers A list of `sfl` objects. Skimmers defined using |
| 211 |
#' `skim_with()` |
| 212 |
#' @param append Same as above. |
| 213 |
#' @param base Same as above. |
| 214 |
#' @noRd |
| 215 |
get_final_skimmers <- function(column, data, local_skimmers, append) { |
| 216 |
defaults <- get_skimmers(data[[column]]) |
| 217 |
all_classes <- skim_class(data[[column]]) |
| 218 |
locals <- get_local_skimmers(all_classes, local_skimmers) |
| 219 |
|
| 220 |
if (!nzchar(defaults$skim_type)) { |
| 221 |
msg <- sprintf( |
| 222 |
"Default skimming functions for column [%s] with class [%s]", |
| 223 |
column, paste(all_classes, collapse = ", ") |
| 224 |
) |
| 225 |
stop(msg, " did not have value for its `.class` argument. Please ", |
| 226 |
"investigate the definition of the associated S3 method.", |
| 227 |
call. = FALSE |
| 228 |
) |
| 229 |
} |
| 230 |
|
| 231 |
if (is.null(locals$funs)) { |
| 232 |
if (defaults$skim_type == "default") { |
| 233 |
msg <- sprintf( |
| 234 |
"Couldn't find skimmers for class: %s;", |
| 235 |
paste(all_classes, collapse = ", ") |
| 236 |
) |
| 237 |
warning(msg, |
| 238 |
" No user-defined `sfl` provided. Falling back to `character`.", |
| 239 |
call. = FALSE |
| 240 |
) |
| 241 |
data[[column]] <- as.character(data[[column]]) |
| 242 |
skimmers <- defaults |
| 243 |
skimmers$skim_type <- "character" |
| 244 |
} else { |
| 245 |
skimmers <- defaults |
| 246 |
} |
| 247 |
} else { |
| 248 |
skimmers <- merge_skimmers(locals, defaults, append) |
| 249 |
} |
| 250 |
skimmers |
| 251 |
} |
| 252 |
|
| 253 |
skim_class <- function(column) { |
| 254 |
base_class <- class(column) |
| 255 |
if (any(base_class %in% c("double", "integer"))) { |
| 256 |
c(base_class, "numeric") |
| 257 |
} else { |
| 258 |
base_class |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
get_local_skimmers <- function(classes, local_skimmers) { |
| 263 |
local_classes <- intersect(classes, names(local_skimmers)) |
| 264 |
if (length(local_classes) == 0) { |
| 265 |
return(NULL) |
| 266 |
} |
| 267 |
|
| 268 |
first_class <- local_classes[[1]] |
| 269 |
out <- local_skimmers[[first_class]] |
| 270 |
out$skim_type <- first_class |
| 271 |
out |
| 272 |
} |
| 273 |
|
| 274 |
merge_skimmers <- function(locals, defaults, append) { |
| 275 |
if (!append || locals$skim_type != defaults$skim_type) { |
| 276 |
locals |
| 277 |
} else { |
| 278 |
defaults$funs <- purrr::compact(purrr::list_modify( |
| 279 |
defaults$funs, !!!locals$funs |
| 280 |
)) |
| 281 |
defaults |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
reduce_skimmers <- function(skimmers, types) { |
| 286 |
named <- rlang::set_names(skimmers, types) |
| 287 |
named[unique(types)] |
| 288 |
} |
| 289 |
|
| 290 |
join_with_base <- function(skimmers, base) { |
| 291 |
skimmers$funs <- c(base$funs, skimmers$funs) |
| 292 |
skimmers |
| 293 |
} |
| 294 |
|
| 295 |
get_skimmers_used <- function(skimmers) { |
| 296 |
types <- names(skimmers) |
| 297 |
function_names <- purrr::map(skimmers, ~ names(.x$funs)) |
| 298 |
rlang::set_names(function_names, types) |
| 299 |
} |
| 300 |
|
| 301 |
NAME_DELIMETER <- "~!@#$%^&*()-+" |
| 302 |
mangle_names <- function(skimmers, base_names) { |
| 303 |
fun_names <- names(skimmers$funs) |
| 304 |
prefixes <- ifelse( |
| 305 |
fun_names %in% base_names, |
| 306 |
NAME_DELIMETER, |
| 307 |
paste0(NAME_DELIMETER, skimmers$skim_type, ".") |
| 308 |
) |
| 309 |
mangled <- paste0(prefixes, fun_names) |
| 310 |
skimmers$funs <- rlang::set_names(skimmers$funs, mangled) |
| 311 |
skimmers |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
#' Generate one or more rows of a `skim_df`, using one column |
| 316 |
#' |
| 317 |
#' Call all of the skimming functions on the single column, using grouped |
| 318 |
#' variants, if necessary. |
| 319 |
#' |
| 320 |
#' We expect one row per variable/ group. To do this we need to take the |
| 321 |
#' processed results, find the appropriate columns for each variable and |
| 322 |
#' restack them. This uses a small hack that rests on the naming convention |
| 323 |
#' of data frame produced by `summarize_at`, which uses the following scheme: |
| 324 |
#' |
| 325 |
#' - `variable_name` + `_` + `function_name` |
| 326 |
#' |
| 327 |
#' To avoid inappropriately assigning the columns to the wrong variable, we |
| 328 |
#' mangle the function names. That way, each set of relevant columns begin |
| 329 |
#' with the column name + `_` + our internal delimiter. |
| 330 |
#' |
| 331 |
#' @param mangled_skimmers The `sfl`'s whose function names have been mangled. |
| 332 |
#' @param variable_names The names of columns in the original data, matching a |
| 333 |
#' data type, that will be summarized. |
| 334 |
#' @param data The original data. |
| 335 |
#' @keywords internal |
| 336 |
#' @noRd |
| 337 |
skim_by_type <- function(mangled_skimmers, variable_names, data) { |
| 338 |
UseMethod("skim_by_type", data) |
| 339 |
} |
| 340 |
|
| 341 |
#' @export |
| 342 |
skim_by_type.grouped_df <- function(mangled_skimmers, variable_names, data) { |
| 343 |
group_columns <- dplyr::groups(data) |
| 344 |
grouped <- dplyr::group_by(data, !!!group_columns) |
| 345 |
skimmed <- dplyr::summarize( |
| 346 |
grouped, |
| 347 |
dplyr::across(tidyselect::any_of(variable_names), mangled_skimmers$funs) |
| 348 |
) |
| 349 |
build_results(skimmed, variable_names, group_columns) |
| 350 |
} |
| 351 |
|
| 352 |
#' @export |
| 353 |
skim_by_type.data.frame <- function(mangled_skimmers, variable_names, data) { |
| 354 |
skimmed <- dplyr::summarize( |
| 355 |
data, |
| 356 |
dplyr::across(tidyselect::any_of(variable_names), mangled_skimmers$funs) |
| 357 |
) |
| 358 |
build_results(skimmed, variable_names, NULL) |
| 359 |
} |
| 360 |
|
| 361 |
#' @export |
| 362 |
skim_by_type.data.table <- function(mangled_skimmers, variable_names, data) { |
| 363 |
data <- tibble::as_tibble(data) |
| 364 |
skimmed <- dplyr::summarize( |
| 365 |
data, |
| 366 |
dplyr::across(tidyselect::any_of(variable_names), mangled_skimmers$funs) |
| 367 |
) |
| 368 |
build_results(skimmed, variable_names, NULL) |
| 369 |
} |
| 370 |
|
| 371 |
#' Summarize returns a single row data frame, make it tall. |
| 372 |
#' @noRd |
| 373 |
build_results <- function(skimmed, variable_names, groups) { |
| 374 |
if (length(variable_names) > 1) { |
| 375 |
out <- tibble::tibble( |
| 376 |
skim_variable = variable_names, |
| 377 |
by_variable = purrr::map(variable_names, reshape_skimmed, skimmed, groups) |
| 378 |
) |
| 379 |
tidyr::unnest(out, "by_variable") |
| 380 |
} else { |
| 381 |
out <- dplyr::select( |
| 382 |
as.data.frame(skimmed), |
| 383 |
!!!groups, |
| 384 |
tidyselect::contains(NAME_DELIMETER) |
| 385 |
) |
| 386 |
tibble::tibble( |
| 387 |
skim_variable = variable_names, |
| 388 |
!!!set_clean_names(out) |
| 389 |
) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
reshape_skimmed <- function(column, skimmed, groups) { |
| 394 |
delim_name <- paste0(column, "_", NAME_DELIMETER) |
| 395 |
out <- dplyr::select( |
| 396 |
tibble::as_tibble(skimmed), |
| 397 |
!!!groups, |
| 398 |
tidyselect::starts_with(delim_name, ignore.case = FALSE) |
| 399 |
) |
| 400 |
set_clean_names(out) |
| 401 |
} |
| 402 |
|
| 403 |
set_clean_names <- function(out) { |
| 404 |
separated <- strsplit(names(out), NAME_DELIMETER, fixed = TRUE) |
| 405 |
clean_names <- purrr::map_chr(separated, ~ .x[length(.x)]) |
| 406 |
rlang::set_names(out, clean_names) |
| 407 |
} |