From dc76b1f08863ce5ef64a1920c1380ab210ff5b39 Mon Sep 17 00:00:00 2001 From: Michael Quinn Date: Wed, 6 Nov 2019 21:00:54 -0500 Subject: [PATCH 1/2] Add option for stripping tibble metadata. --- R/skim_print.R | 36 ++++++++++++++++++++++++++---------- man/print.Rd | 6 ++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/R/skim_print.R b/R/skim_print.R index 09d6ed35..0131ccbf 100644 --- a/R/skim_print.R +++ b/R/skim_print.R @@ -18,17 +18,25 @@ #' #' @inheritParams tibble:::print.tbl #' @param include_summary Whether a summary of the data frame should be printed +#' @param strip_metadata Whether tibble metadata should be removed. #' @name print NULL #' @describeIn print Print a skimmed data frame (`skim_df` from [skim()]). #' @export -print.skim_df <- function(x, include_summary = TRUE, n = Inf, width = Inf, - n_extra = NULL, ...) { +print.skim_df <- function(x, + include_summary = TRUE, + n = Inf, + width = Inf, + n_extra = NULL, + strip_metadata = TRUE, + ...) { if (is_skim_df(x)) { - if (include_summary) print(summary(x)) + if (include_summary) { + print(summary(x)) + } by_type <- partition(x) - purrr::imap(by_type, print, n, width, n_extra) + purrr::map(by_type, print, n, width, n_extra, strip_metadata, ...) invisible(NULL) } else { NextMethod("print") @@ -37,12 +45,20 @@ print.skim_df <- function(x, include_summary = TRUE, n = Inf, width = Inf, #' @describeIn print Print an entry within a partitioned `skim_df`. #' @export -print.one_skim_df <- function(x, n = Inf, width = Inf, n_extra = NULL, ...) { +print.one_skim_df <- function(x, + n = Inf, + width = Inf, + n_extra = NULL, + strip_metadata = TRUE, + ...) { variable_type <- paste("Variable type:", attr(x, "skim_type")) top_line <- cli::rule(line = 1, left = variable_type) - dots <- list(...) - out <- format(x, n = n, width = width, n_extra = n_extra, dots) - metadata <- grab_tibble_metadata(out) + out <- format(x, ..., n = n, width = width, n_extra = n_extra, dots) + if (strip_metadata) { + metadata <- -1 * grab_tibble_metadata(out) + } else { + metadata <- seq_along(out) + } render_skim_body(top_line, out, metadata) } @@ -54,8 +70,8 @@ grab_tibble_metadata <- function(x) { } } -render_skim_body <- function(top_line, out, metadata) { - cat(paste0("\n", top_line), out[-metadata], sep = "\n") +render_skim_body <- function(top_line, out, metadata_to_remove) { + cat(paste0("\n", top_line), out[metadata_to_remove], sep = "\n") } #' @describeIn print Print a `skim_list`, a list of `skim_df` objects. diff --git a/man/print.Rd b/man/print.Rd index b27c9da4..1a7b002c 100644 --- a/man/print.Rd +++ b/man/print.Rd @@ -9,10 +9,10 @@ \title{Print \code{skim} objects} \usage{ \method{print}{skim_df}(x, include_summary = TRUE, n = Inf, - width = Inf, n_extra = NULL, ...) + width = Inf, n_extra = NULL, strip_metadata = TRUE, ...) \method{print}{one_skim_df}(x, n = Inf, width = Inf, n_extra = NULL, - ...) + strip_metadata = TRUE, ...) \method{print}{skim_list}(x, n = Inf, width = Inf, n_extra = NULL, ...) @@ -38,6 +38,8 @@ default and always print all columns.} if the width is too small for the entire tibble. If \code{NULL}, the default, will print information about at most \code{tibble.max_extra_cols} extra columns.} +\item{strip_metadata}{Whether tbl metadata should be removed.} + \item{...}{Other arguments passed on to individual methods.} } \description{ From 8da0589c838cb3c3466fb8d0d14aec10fca6129c Mon Sep 17 00:00:00 2001 From: Michael Quinn Date: Thu, 7 Nov 2019 14:17:12 -0500 Subject: [PATCH 2/2] Remove unused variable --- R/skim_print.R | 2 +- man/print.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/skim_print.R b/R/skim_print.R index 0131ccbf..5dbd4eaa 100644 --- a/R/skim_print.R +++ b/R/skim_print.R @@ -53,7 +53,7 @@ print.one_skim_df <- function(x, ...) { variable_type <- paste("Variable type:", attr(x, "skim_type")) top_line <- cli::rule(line = 1, left = variable_type) - out <- format(x, ..., n = n, width = width, n_extra = n_extra, dots) + out <- format(x, ..., n = n, width = width, n_extra = n_extra) if (strip_metadata) { metadata <- -1 * grab_tibble_metadata(out) } else { diff --git a/man/print.Rd b/man/print.Rd index 1a7b002c..b553cd04 100644 --- a/man/print.Rd +++ b/man/print.Rd @@ -38,7 +38,7 @@ default and always print all columns.} if the width is too small for the entire tibble. If \code{NULL}, the default, will print information about at most \code{tibble.max_extra_cols} extra columns.} -\item{strip_metadata}{Whether tbl metadata should be removed.} +\item{strip_metadata}{Whether tibble metadata should be removed.} \item{...}{Other arguments passed on to individual methods.} }