Debian Codegraph @

R/skim_print.R

raw
1 #' Print `skim` objects
2 #'
3 #' `skimr` has custom print methods for all supported objects. Default printing
4 #' methods for `knitr`/ `rmarkdown` documents is also provided.
5 #'
6 #' @section Printing options:
7 #'
8 #' For better or for worse, `skimr` often produces more output than can fit in
9 #' the standard R console. Fortunately, most modern environments like RStudio
10 #' and Jupyter support more than 80 character outputs. Call
11 #' `options(width = 90)` to get a better experience with `skimr`.
12 #'
13 #' The print methods in `skimr` wrap those in the [tibble][tibble::formatting]
14 #' package. You can control printing behavior using the same global options.
15 #'
16 #' @section Behavior in `dplyr` pipelines:
17 #'
18 #' Printing a `skim_df` requires specific columns that might be dropped when
19 #' using [dplyr::select()] or [dplyr::summarize()] on a `skim_df`. In those
20 #' cases, this method falls back to [tibble::print.tbl()].
21 #'
22 #' @section Options for controlling print behavior:
23 #'
24 #' You can control the width rule line for the printed subtables with an option:
25 #' `skimr_table_header_width`.
26 #'
27 #' @inheritParams tibble::print.tbl
28 #' @seealso [tibble::trunc_mat()] For a list of global options for customizing
29 #' print formatting. [cli::num_ansi_colors()] for the variety of issues that
30 #' affect tibble's color support.
31 #' @param include_summary Whether a summary of the data frame should be printed
32 #' @param summary_rule_width Width of Data Summary cli rule, defaults to 40.
33 #' @name print
34 NULL
35
36 #' @describeIn print Print a skimmed data frame (`skim_df` from [skim()]).
37 #' @export
38 print.skim_df <- function(x,
39 include_summary = TRUE,
40 n = Inf,
41 width = Inf,
42 summary_rule_width = getOption(
43 "skimr_summary_rule_width",
44 default = 40
45 ),
46 ...) {
47 if (is_skim_df(x) && nrow(x) > 0) {
48 if (include_summary) {
49 print(summary(x), .summary_rule_width = summary_rule_width, ...)
50 }
51 by_type <- partition(x)
52 purrr::map(
53 by_type,
54 print,
55 width = width,
56 n = n,
57 ...
58 )
59 invisible(x)
60 } else {
61 NextMethod("print")
62 }
63 }
64
65 # Methods for correctly formatting a `one_skim_df`. We leverage the
66 # customiztion options in `pillar` for this. It divides the results into: a
67 # header, which we customize; a body, where we strip some values; and a footer,
68 # which we drop. For more details, see
69 # https://pillar.r-lib.org/articles/extending.html
70
71 #' @importFrom pillar tbl_format_header
72 #' @export
73 tbl_format_header.one_skim_df <- function(x, setup, ...) {
74 variable_type <- paste("Variable type:", attr(x, "skim_type"))
75 rule <- cli::rule(
76 line = 1,
77 left = variable_type,
78 width = getOption("skimr_table_header_width", getOption("width", 80))
79 )
80 # Add an empty line before the rule
81 c("", rule)
82 }
83
84 #' @importFrom pillar ctl_new_pillar
85 #' @export
86 ctl_new_pillar.one_skim_df <- function(controller,
87 x,
88 width,
89 ...,
90 title = NULL) {
91 out <- NextMethod()
92 out$type <- NULL
93 out
94 }
95
96 #' @describeIn print Print a `skim_list`, a list of `skim_df` objects.
97 #' @export
98 print.skim_list <- function(x,
99 n = Inf,
100 width = Inf,
101 ...) {
102 nms <- names(x)
103 attributes(x) <- NULL
104 print(rlang::set_names(x, nms))
105 }
106
107
108 #' @describeIn print Print method for a `summary_skim_df` object.
109 #' @param .summary_rule_width the width for the main rule above the summary.
110 #' @param ... Passed on to \link[pillar:tbl_format_setup]{pillar::tbl_format_setup()}
111 #' @export
112 print.summary_skim_df <- function(x, .summary_rule_width = 40, ...) {
113 with_title <- c(
114 cli::rule(line = 1, left = "Data Summary", width = .summary_rule_width),
115 format(x)
116 )
117 writeLines(with_title)
118 }
119
120 #' Provide a default printing method for knitr.
121 #'
122 #' Instead of standard R output, `knitr` and `RMarkdown` documents will have
123 #' formatted [knitr::kable()] output on return. You can disable this by setting
124 #' the chunk option `render = normal_print`.
125 #'
126 #' The summary statistics for the original data frame can be disabled by setting
127 #' the `knitr` chunk option `skimr_include_summary = FALSE`. See
128 #' [knitr::opts_chunk] for more information. You can change the number of digits
129 #' shown in the printed table with the `skimr_digits` chunk option.
130 #'
131 #' Alternatively, you can call [yank()] to get the particular
132 #' `skim_df` objects and format them however you like. One warning though.
133 #' Because histograms contain unicode characters, they can have unexpected
134 #' print results, as R as varying levels of unicode support. This affects
135 #' Windows users most commonly. Call `vignette("Using_fonts")` for more details.
136 #'
137 #' @seealso [knitr::kable()]
138 #' @inheritParams knitr::knit_print
139 #' @param options Options passed into the print function.
140 #' @return A `knit_asis` object. Which is used by `knitr` when rendered.
141 #' @importFrom knitr knit_print
142 #' @name knit_print
143 NULL
144
145 #' @describeIn knit_print Default `knitr` print for `skim_df` objects.
146 #' @export
147 knit_print.skim_df <- function(x, options = NULL, ...) {
148 if (is_skim_df(x) && nrow(x) > 0) {
149 if (options$skimr_include_summary %||% TRUE) {
150 summary_stats <- summary(x)
151 kabled <- knit_print(summary_stats)
152 } else {
153 kabled <- c()
154 }
155
156 by_type <- partition(x)
157 knit_print_by_type(by_type, options, kabled)
158 } else {
159 NextMethod("knit_print")
160 }
161 }
162
163 knit_print_by_type <- function(x, options, summary) {
164 all_tables <- purrr::imap(x, knit_print_one, options)
165 combined <- c("", summary, "", "", unlist(all_tables))
166 knitr::asis_output(paste(combined, collapse = "\n"))
167 }
168
169 knit_print_one <- function(by_type, type, options) {
170 kabled <- knitr::kable(
171 by_type,
172 digits = options$skimr_digits %||% 2
173 )
174 caption <- sprintf("**Variable type: %s**", type)
175 c(caption, "", kabled, "", "")
176 }
177
178 #' @describeIn knit_print Default `knitr` print for a `skim_list`.
179 #' @export
180 knit_print.skim_list <- function(x, options = NULL, ...) {
181 knit_print_by_type(x, options, NULL)
182 }
183
184 #' @describeIn knit_print Default `knitr` print within a partitioned `skim_df`.
185 #' @export
186 knit_print.one_skim_df <- function(x, options = NULL, ...) {
187 kabled <- knit_print_one(x, attr(x, "skim_type"), options)
188 combined <- c("", "", kabled, "")
189 knitr::asis_output(paste(combined, collapse = "\n"))
190 }
191
192 #' @describeIn knit_print Default `knitr` print for `skim_df` summaries.
193 #' @export
194 knit_print.summary_skim_df <- function(x, options = NULL, ...) {
195 summary_mat <- cbind(
196 get_summary_dnames(x),
197 get_summary_values(x),
198 deparse.level = 0
199 )
200 kabled <- knitr::kable(
201 summary_mat,
202 table.attr = "style='width: auto;'
203 class='table table-condensed'",
204 caption = "Data summary"
205 )
206
207 knitr::asis_output(paste(kabled, collapse = "\n"))
208 }
209
210
211 #' Skimr printing within Jupyter notebooks
212 #'
213 #' This reproduces printed results in the console. By default Jupyter kernels
214 #' render the final object in the cell. We want the version printed by
215 #' `skimr` instead of the data that it contains.
216 #'
217 #' @param obj The object to \link{print} and then return the output.
218 #' @param ... ignored.
219 #' @return None. `invisible(NULL)`.
220 #' @importFrom repr repr_text
221 #' @name repr
222
223 #' @rdname repr
224 #' @export
225 repr_text.skim_df <- function(obj, ...) {
226 paste(utils::capture.output(print(obj)), collapse = "\n")
227 }
228
229 #' @rdname repr
230 #' @export
231 repr_text.skim_list <- repr_text.skim_df
232
233 #' @rdname repr
234 #' @export
235 repr_text.one_skim_df <- repr_text.skim_df