-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathglimpse.R
188 lines (166 loc) · 5.38 KB
/
glimpse.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#' Get a glimpse of your data
#'
#' @description
#' `glimpse()` is like a transposed version of `print()`:
#' columns run down the page, and data runs across.
#' This makes it possible to see every column in a data frame.
#' It's a little like [str()] applied to a data frame
#' but it tries to show you as much data as possible.
#' (And it always shows the underlying data, even when applied
#' to a remote data source.)
#'
#' See [format_glimpse()] for details on the formatting.
#'
#' @section S3 methods:
#' `glimpse` is an S3 generic with a customised method for `tbl`s and
#' `data.frames`, and a default method that calls [str()].
#'
#' @param x An object to glimpse at.
#' @param width Width of output: defaults to the setting of the
#' `width` [option][pillar_options] (if finite)
#' or the width of the console.
#' @param ... Unused, for extensibility.
#' @return x original x is (invisibly) returned, allowing `glimpse()` to be
#' used within a data pipe line.
#' @export
#' @examples
#' glimpse(mtcars)
#'
#' @examplesIf rlang::is_installed("nycflights13")
#' glimpse(nycflights13::flights)
#'
glimpse <- function(x, width = NULL, ...) {
UseMethod("glimpse")
}
#' @export
glimpse.tbl <- function(x, width = NULL, ...) {
if (!is.null(width) && !is.finite(width)) {
abort("`width` must be finite.")
}
width <- get_width_glimpse(width)
cli::cat_line("Rows: ", big_mark(nrow(x)))
# this is an overestimate, but shouldn't be too expensive.
# every type needs at least three characters: "x, "
rows <- as.integer(width / 3)
df <- df_head(x, rows)
cli::cat_line("Columns: ", big_mark(ncol(df)))
summary <- tbl_sum(x)
brief_summary <- summary[-1]
if (has_length(brief_summary)) {
cli::cat_line(names(brief_summary), ": ", brief_summary)
}
if (ncol(df) == 0) {
return(invisible(x))
}
var_types <- map_chr(map(df, new_pillar_type), format)
ticked_names <- format(new_pillar_title(tick_if_needed(names(df))))
var_names <- paste0("$ ", align(ticked_names), " ", var_types, " ")
# for some reason the offset was -2 in tibble but is now -1
# so that the desired width is obtained
data_width <- width - get_extent(var_names) - 1
formatted <- map_chr(df, format_glimpse_1)
truncated <- str_trunc(formatted, data_width)
cli::cat_line(var_names, truncated)
invisible(x)
}
#' @export
glimpse.data.frame <- glimpse.tbl
#' @importFrom utils str
#' @export
glimpse.default <- function(x, width = NULL, max.level = 3, ...) {
str(x, width = get_width_glimpse(width), max.level = max.level, ...)
invisible(x)
}
#' Format a vector for horizontal printing
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' This generic provides the logic for printing vectors in [glimpse()].
#'
#' The output strives to be as unambiguous as possible,
#' without compromising on readability.
#' In a list, to distinguish between vectors and nested lists,
#' the latter are surrounded by `[]` brackets.
#' Empty lists are shown as `[]`.
#' Vectors inside lists, of length not equal to one,
#' are surrounded by `<>` angle brackets.
#' Empty vectors are shown as `<>`.
#'
#' @return A character vector of the same length as `x`.
#' @inheritParams rlang::args_dots_used
#' @param x A vector.
#' @export
#' @examples
#' format_glimpse(1:3)
#'
#' # Lists use [], vectors inside lists use <>
#' format_glimpse(list(1:3))
#' format_glimpse(list(1, 2:3))
#' format_glimpse(list(list(1), list(2:3)))
#' format_glimpse(list(as.list(1), as.list(2:3)))
#' format_glimpse(list(character()))
#' format_glimpse(list(NULL))
#'
#' # Character strings are always quoted
#' writeLines(format_glimpse(letters[1:3]))
#' writeLines(format_glimpse(c("A", "B, C")))
#'
#' # Factors are quoted only when needed
#' writeLines(format_glimpse(factor(letters[1:3])))
#' writeLines(format_glimpse(factor(c("A", "B, C"))))
format_glimpse <- function(x, ...) {
check_dots_used()
UseMethod("format_glimpse")
}
# A variant with collapse and without checks, for format_glimpse.list()
format_glimpse_1 <- function(x, ..., .inner = NULL) {
if (!is_null(.inner) && .inner) {
collapse(format_glimpse_(x, ..., .inner = .inner))
} else {
paste0(format_glimpse_(x, ...), collapse = crayon_grey_0.6(", "))
}
}
format_glimpse_ <- function(x, ...) {
UseMethod("format_glimpse")
}
#' @export
format_glimpse.default <- function(x, ...) {
dims <- dim(x)
if (is.null(dims)) {
out <- format(x, trim = TRUE, justify = "none")
style_na_if(out, if (is.atomic(x)) is.na(x) else FALSE)
} else {
dims_out <- paste0(dims, collapse = " x ")
paste0("<", class(x)[[1]], "[", dims_out, "]>")
}
}
#' @export
format_glimpse.list <- function(x, ..., .inner = FALSE) {
if (!.inner && length(x) == 0) {
return("list()")
}
out <- map_chr(x, format_glimpse_1, .inner = TRUE)
# Surround vectors by <>
# Only surround inner lists by []
list <- map_lgl(x, is.list)
scalar <- rep_along(x, TRUE)
scalar[!list] <- (map_int(x[!list], length) == 1L)
out[!scalar] <- paste0("<", out[!scalar], ">")
out[list] <- paste0("[", out[list], "]")
out
}
#' @export
format_glimpse.character <- function(x, ...) {
out <- encodeString(as.character(x), quote = '"')
style_na_if(out, is.na(x))
}
#' @export
format_glimpse.factor <- function(x, ...) {
if (any(grepl(",", levels(x), fixed = TRUE))) {
out <- encodeString(as.character(x), quote = '"')
} else {
out <- format(x, trim = TRUE, justify = "none")
}
style_na_if(out, is.na(x))
}