Skip to content

Commit

Permalink
Make glimpse a generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Nov 4, 2015
1 parent 863f9ca commit 3b5f3a6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ S3method(format_v,character)
S3method(format_v,default)
S3method(full_join,data.frame)
S3method(full_join,tbl_df)
S3method(glimpse,data.frame)
S3method(glimpse,default)
S3method(glimpse,tbl)
S3method(group_by_,data.frame)
S3method(group_by_,data.table)
S3method(group_by_,rowwise_df)
Expand Down
32 changes: 25 additions & 7 deletions R/glimpse.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
#' 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.)
#'
#' @param tbl A data table
#' @section S3 methods:
#' \code{glimpse} is an S3 generic with a customised method for \code{tbl}s and
#' \code{data.frames}, and a default method that calls \code{\link{str}}.
#'
#' @param x An object to glimpse at.
#' @param width Width of output: defaults to the width of the console.
#' @param ... Other arguments passed onto individual methods.
#' @export
#' @examples
#' glimpse(mtcars)
Expand All @@ -18,29 +23,42 @@
#' glimpse(batting)
#' }
#' }
glimpse <- function(tbl, width = getOption("width")) {
cat("Observations: ", big_mark(nrow(tbl)), "\n", sep = "")
if (ncol(tbl) == 0) return(invisible())
glimpse <- function(x, width = getOption("width"), ...) {
UseMethod("glimpse")
}

cat("Variables: ", big_mark(ncol(tbl)), "\n", sep = "")
#' @export
glimpse.tbl <- function(x, width = getOption("width"), ...) {
cat("Observations: ", big_mark(nrow(x)), "\n", sep = "")
if (ncol(x) == 0) return(invisible())

cat("Variables: ", big_mark(ncol(x)), "\n", sep = "")

# this is an overestimate, but shouldn't be too expensive.
# every type needs at least three characters: "x, "
rows <- as.integer(width / 3)
df <- as.data.frame(head(tbl, rows))
df <- as.data.frame(head(x, rows))

var_types <- vapply(df, type_sum, character(1))
var_names <- paste0("$ ", format(names(df)), " (", var_types, ") ")

data_width <- width - nchar(var_names) - 2

formatted <- vapply(df, function(x) paste0(format_v(x), collapse = ", "),
character(1), USE.NAMES = FALSE)
truncated <- str_trunc(formatted, data_width)

cat(paste0(var_names, truncated, collapse = "\n"), "\n", sep = "")
}

#' @export
glimpse.data.frame <- glimpse.tbl

#' @export
glimpse.default <- function(x, width = getOption("width"), max.level = 3, ...) {
str(x, width = width, max.level = max.level, ...)
}

str_trunc <- function(x, max_width) {
width <- nchar(x)

Expand Down
11 changes: 9 additions & 2 deletions man/glimpse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3b5f3a6

Please sign in to comment.