Skip to content

Commit

Permalink
Pull prefix/suffix out obj_sum.
Browse files Browse the repository at this point in the history
And consistently use <> for types everywhere.
  • Loading branch information
hadley committed Mar 18, 2016
1 parent d44b593 commit c93ee40
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion R/type-sum.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
obj_sum <- function(x) UseMethod("obj_sum")
#' @export
obj_sum.default <- function(x) {
paste0("<", type_sum(x), if (is_vector_s3(x)) size_sum(x), ">")
paste0(type_sum(x), if (is_vector_s3(x)) size_sum(x))
}

#' @export
Expand Down
11 changes: 7 additions & 4 deletions R/utils-format.r
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ shrink_mat <- function(df, width, n_extra, var_names, var_types, rows, n) {
}

# List columns need special treatment because format can't be trusted
classes <- paste0("(", vapply(df, type_sum, character(1)), ")")
classes <- paste0("<", vapply(df, type_sum, character(1)), ">")
is_list <- vapply(df, is.list, logical(1))
df[is_list] <- lapply(df[is_list], function(x) vapply(x, obj_sum, character(1)))
df[is_list] <- lapply(df[is_list], function(x) {
summary <- vapply(x, obj_sum, character(1))
paste0("<", summary, ">")
})

mat <- format(df, justify = "left")
values <- c(format(rownames(mat))[[1]], unlist(mat[1, ]))
Expand Down Expand Up @@ -133,7 +136,7 @@ print.trunc_mat <- function(x, ...) {
}

if (length(x$extra) > 0) {
var_types <- paste0(names(x$extra), " (", x$extra, ")", collapse = ", ")
var_types <- paste0(names(x$extra), " <", x$extra, ">", collapse = ", ")
cat(wrap("Variables not shown: ", var_types, width = x$width),
".\n", sep = "")
}
Expand All @@ -147,7 +150,7 @@ knit_print.trunc_mat <- function(x, options) {
kable <- knitr::kable(x$table, row.names = FALSE)

if (length(x$extra) > 0) {
var_types <- paste0(names(x$extra), " (", x$extra, ")", collapse = ", ")
var_types <- paste0(names(x$extra), " <", x$extra, ">", collapse = ", ")
extra <- wrap("\n(_Variables not shown_: ", var_types, ")", width = x$width)
} else {
extra <- "\n"
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-obj-sum.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ context("obj_sum")

test_that("shows only first class name for S4", {
A <- methods::setClass("A")
expect_equal(obj_sum(A), "<S4: classGeneratorFunction>")
expect_equal(obj_sum(A), "S4: classGeneratorFunction")
})

test_that("NULL handled specially", {
expect_equal(obj_sum(NULL), "<NULL>")
expect_equal(obj_sum(NULL), "NULL")
})

test_that("data frame includes rows and cols", {
expect_equal(obj_sum(mtcars), "<data.frame [32,11]>")
expect_equal(obj_sum(mtcars), "data.frame [32,11]")
})

test_that("S3 others list all classes", {
x <- structure(list(), class = c("a", "b", "c"))
expect_equal(obj_sum(x), "<S3: a/b/c>")
expect_equal(obj_sum(x), "S3: a/b/c")
})

test_that("common data vectors treated as atomic", {
expect_equal(obj_sum(factor(1:3)), "<fctr [3]>")
expect_equal(obj_sum(Sys.Date() + 1:3), "<date [3]>")
expect_equal(obj_sum(Sys.time() + 1:3), "<time [3]>")
expect_equal(obj_sum(factor(1:3)), "fctr [3]")
expect_equal(obj_sum(Sys.Date() + 1:3), "date [3]")
expect_equal(obj_sum(Sys.time() + 1:3), "time [3]")
})


Expand Down
38 changes: 19 additions & 19 deletions tests/testthat/test-trunc-mat.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("trunc_mat output matches known output", {
capture.output(print(tbl_df(mtcars), n = 8L, width = 30L)),
c("Source: local data frame [32 x 11]", "",
" mpg cyl disp hp",
" (dbl) (dbl) (dbl) (dbl)",
" <dbl> <dbl> <dbl> <dbl>",
"1 21.0 6 160.0 110",
"2 21.0 6 160.0 110",
"3 22.8 4 108.0 93",
Expand All @@ -16,33 +16,33 @@ test_that("trunc_mat output matches known output", {
"8 24.4 4 146.7 62",
".. ... ... ... ...",
"Variables not shown: drat",
" (dbl), wt (dbl), qsec",
" (dbl), vs (dbl), am (dbl),",
" gear (dbl), carb (dbl)."
" <dbl>, wt <dbl>, qsec",
" <dbl>, vs <dbl>, am <dbl>,",
" gear <dbl>, carb <dbl>."
)
)

expect_identical(
capture.output(print(tbl_df(iris), n = 5L, width = 30L)),
c("Source: local data frame [150 x 5]", "",
" Sepal.Length Sepal.Width",
" (dbl) (dbl)",
" <dbl> <dbl>",
"1 5.1 3.5",
"2 4.9 3.0",
"3 4.7 3.2",
"4 4.6 3.1",
"5 5.0 3.6",
".. ... ...",
"Variables not shown:",
" Petal.Length (dbl),",
" Petal.Width (dbl), Species",
" (fctr)."))
" Petal.Length <dbl>,",
" Petal.Width <dbl>, Species",
" <fctr>."))

expect_identical(
capture.output(print(tbl_df(iris), n = 3L, width = 5L))[1:8],
c("Source: local data frame [150 x 5]", "",
" Sepal.Length",
" (dbl)",
" <dbl>",
"1 5.1",
"2 4.9",
"3 4.7",
Expand All @@ -52,20 +52,20 @@ test_that("trunc_mat output matches known output", {
capture.output(print(df_all, n = NULL, width = 30L)),
c("Source: local data frame [2 x 8]", "",
" a b c d",
" (dbl) (int) (lgl) (chr)",
" <dbl> <int> <lgl> <chr>",
"1 1.0 1 TRUE a",
"2 2.5 2 FALSE b",
"Variables not shown: e",
" (fctr), f (date), g (time),",
" h (list)."))
" <fctr>, f <date>, g <time>,",
" h <list>."))

expect_identical(
capture.output(print(data_frame(a = character(), b = logical()),
width = 30L)),
c("Source: local data frame [0 x 2]",
"",
"Variables not shown: a (chr),",
" b (lgl).")
"Variables not shown: a <chr>,",
" b <lgl>.")
)

expect_identical(
Expand All @@ -77,12 +77,12 @@ test_that("trunc_mat output matches known output", {
expect_identical(
capture.output(trunc_mat(df_all, n = 1L, n_extra = 2L, width = 30L)),
c(" a b c d",
" (dbl) (int) (lgl) (chr)",
" <dbl> <int> <lgl> <chr>",
"1 1 1 TRUE a",
".. ... ... ... ...",
"Variables not shown: e",
" (fctr), f (date), and 2",
" more (...)."))
" <fctr>, f <date>, and 2",
" more <...>."))

expect_identical(
knitr::knit_print(trunc_mat(df_all, width = 60L)),
Expand All @@ -92,11 +92,11 @@ test_that("trunc_mat output matches known output", {
"",
"|a |b |c |d |e |f |",
"|:-----|:-----|:-----|:-----|:------|:----------|",
"|(dbl) |(int) |(lgl) |(chr) |(fctr) |(date) |",
"|<dbl> |<int> |<lgl> |<chr> |<fctr> |<date> |",
"|1.0 |1 |TRUE |a |a |2015-12-10 |",
"|2.5 |2 |FALSE |b |b |2015-12-11 |",
"",
"(_Variables not shown_: g (time), h (list))",
"(_Variables not shown_: g <time>, h <list>)",
sep = "\n"),
class = "knit_asis",
knit_cacheable = TRUE)
Expand Down

0 comments on commit c93ee40

Please sign in to comment.