Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obj_sum() returns short version in attribute #390

Merged
merged 2 commits into from
Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion R/type-sum.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ vec_ptype_abbr.pillar_empty_col <- function(x, ...) {
#' As of pillar v1.6.1, the default method forwards to [vctrs::vec_ptype_abbr()]
#' for vectors and to [type_sum()] for other objects.
#' Previous versions always forwarded to [type_sum()].
#' An attribute named `"short"` in the return value will be picked up by
#' the [pillar_shaft()] method for lists, and used if space is limited.
#'
#' @examples
#' obj_sum(1:10)
#' obj_sum(matrix(1:10))
#' obj_sum(data.frame(a = 1))
#' obj_sum(Sys.Date())
#' obj_sum(Sys.time())
#' obj_sum(mean)
Expand All @@ -87,7 +90,16 @@ obj_sum.default <- function(x) {
if (!vec_is(x)) {
type_sum(x)
} else {
paste(vec_ptype_abbr(x, suffix_shape = FALSE), size_sum(x))
abbr <- vec_ptype_abbr(x, suffix_shape = FALSE)

out <- paste(abbr, size_sum(x))
if (is.array(x)) {
short <- paste0(abbr, "[", cli::symbol$ellipsis, "]")
} else {
short <- abbr
}

structure(out, short = short)
}
}

Expand Down
3 changes: 3 additions & 0 deletions man/type_sum.Rd

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

28 changes: 28 additions & 0 deletions tests/testthat/test-obj-sum.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ test_that("data frames and common data vectors have size summary", {

expect_obj_sum_is_ptype <- function(x) {
obj_sum <- obj_sum(x)
attr(obj_sum, "short") <- NULL

ptype <- vec_ptype_abbr(x, suffix_shape = FALSE)

expect_equal(obj_sum, !!paste_with_space_if_needed(ptype, size_sum(x)))
Expand All @@ -32,9 +34,35 @@ test_that("data frames and common data vectors have size summary", {
expect_obj_sum_is_ptype(Sys.time() - Sys.time() + 1:3)
expect_obj_sum_is_ptype(as.POSIXlt(Sys.time() + 1:3))
expect_obj_sum_is_ptype(list(1, 2:3))
expect_obj_sum_is_ptype(matrix(1:10, ncol = 2))
expect_obj_sum_is_ptype(Titanic)
expect_obj_sum_is_ptype(as_override_size_sum(1:3))
})

test_that("object summary has short version", {
local_override_size_sum()

expect_obj_sum_short_is_ptype <- function(x, suffix = "") {
obj_sum <- attr(obj_sum(x), "short")

ptype <- vec_ptype_abbr(x, suffix_shape = FALSE)

expect_equal(obj_sum, !!paste0(ptype, suffix))
}

expect_obj_sum_short_is_ptype(mtcars)
expect_obj_sum_short_is_ptype(factor(1:3))
expect_obj_sum_short_is_ptype(ordered(1:3))
expect_obj_sum_short_is_ptype(Sys.Date() + 1:3)
expect_obj_sum_short_is_ptype(Sys.time() + 1:3)
expect_obj_sum_short_is_ptype(Sys.time() - Sys.time() + 1:3)
expect_obj_sum_short_is_ptype(as.POSIXlt(Sys.time() + 1:3))
expect_obj_sum_short_is_ptype(list(1, 2:3))
expect_obj_sum_short_is_ptype(matrix(1:10, ncol = 2), paste0("[", cli::symbol$ellipsis, "]"))
expect_obj_sum_short_is_ptype(Titanic, paste0("[", cli::symbol$ellipsis, "]"))
expect_obj_sum_short_is_ptype(as_override_size_sum(1:3))
})


# type_sum ----------------------------------------------------------------

Expand Down