Debian Codegraph @

R/summary.R

raw
1 #' Summary function for skim_df
2 #'
3 #' This is a method of the generic function [summary()].
4 #'
5 #' @param object a skim dataframe.
6 #' @param ... Additional arguments affecting the summary produced. Not used.
7 #' @return A summary of the skim data frame.
8 #' @examples
9 #' a <- skim(mtcars)
10 #' summary(a)
11 #' @export
12 summary.skim_df <- function(object, ...) {
13 if (is.null(object)) {
14 stop("dataframe is null.")
15 }
16 duplicated <- duplicated(object$skim_variable)
17 counts <- table(type = object$skim_type[!duplicated])
18 types <- dimnames(counts)[[1]]
19
20 possible_names <- group_names(object)
21 possible_groups <- if (length(possible_names) > 0) {
22 paste(possible_names, collapse = ", ")
23 } else {
24 "None"
25 }
26
27 structure(
28 list(
29 data_name = process_data_name(object),
30 counts = counts,
31 types = types,
32 possible_groups = possible_groups,
33 dt_key = dt_key(object),
34 data_rows = data_rows(object),
35 data_cols = data_cols(object)
36 ),
37 class = "summary_skim_df"
38 )
39 }
40
41 process_data_name <- function(object) {
42 raw_name <- df_name(object)
43 no_ticks <- gsub("`", "", raw_name)
44 if (no_ticks %in% c(".", ".data")) {
45 "Piped data"
46 } else if (nchar(no_ticks) > 25) {
47 paste0(substring(no_ticks, 1, 25), "...")
48 } else {
49 no_ticks
50 }
51 }
52
53 #' @export
54 format.summary_skim_df <- function(x, ...) {
55 dnames <- c("", get_summary_dnames(x))
56 summary_values <- c("Values", get_summary_values(x))
57 paste(
58 format(dnames),
59 format(summary_values)
60 )
61 }
62
63 get_summary_dnames <- function(summary_object) {
64 c(
65 "Name",
66 "Number of rows ",
67 "Number of columns ",
68 if (!is.na(summary_object$dt_key)) "Key",
69 "_______________________ ",
70 "Column type frequency: ",
71 paste0(" ", summary_object$types),
72 "________________________ ",
73 "Group variables"
74 )
75 }
76
77 get_summary_values <- function(summary_object) {
78 c(
79 summary_object$data_name,
80 summary_object$data_rows,
81 summary_object$data_cols,
82 if (!is.na(summary_object$dt_key)) summary_object$dt_key,
83 " ",
84 " ",
85 unname(summary_object$counts),
86 " ",
87 summary_object$possible_groups
88 )
89 }