Skip to content

Commit

Permalink
Merge pull request #20 from UchidaMizuki/tbl-sum-#19
Browse files Browse the repository at this point in the history
Implement tbl_sum() for dibble (#19)
  • Loading branch information
UchidaMizuki authored Jun 26, 2024
2 parents 8c04d10 + 7489258 commit 1abe887
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ S3method(tbl_format_header,ddf_col)
S3method(tbl_format_header,tbl_ddf)
S3method(tbl_format_setup,ddf_col)
S3method(tbl_format_setup,tbl_ddf)
S3method(tbl_sum,ddf_col)
S3method(tbl_sum,tbl_ddf)
S3method(zeros,array)
S3method(zeros,ddf_col)
S3method(zeros,default)
Expand Down Expand Up @@ -156,5 +158,6 @@ importFrom(pillar,tbl_format_body)
importFrom(pillar,tbl_format_footer)
importFrom(pillar,tbl_format_header)
importFrom(pillar,tbl_format_setup)
importFrom(pillar,tbl_sum)
importFrom(tibble,as_tibble)
importFrom(tidyr,replace_na)
10 changes: 10 additions & 0 deletions R/ddf_col.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ format.ddf_col <- function(x, n = NULL, ...) {
...)
}

#' @export
tbl_sum.ddf_col <- function(x) {
dim_names <- dimnames(x)
dim <- list_sizes_unnamed(dim_names)
size_dim <- prod(dim)

c(`A dibble` = big_mark(size_dim),
`Dimensions` = commas(paste0(names(dim_names), " [", big_mark(dim), "]")))
}

#' @export
tbl_format_setup.ddf_col <- function(x, width = NULL, ..., n = NULL, max_extra_cols = NULL, max_footer_lines = NULL, focus = NULL) {
tbl_format_setup_dibble(x,
Expand Down
1 change: 1 addition & 0 deletions R/dibble-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
#' @importFrom pillar tbl_format_footer
#' @importFrom pillar tbl_format_header
#' @importFrom pillar tbl_format_setup
#' @importFrom pillar tbl_sum
## usethis namespace: end
NULL
26 changes: 4 additions & 22 deletions R/dibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -439,31 +439,13 @@ format_dibble <- function(x, n, ...) {
}

tbl_format_setup_dibble <- function(x, n, ...) {
dim_names <- dimnames(x)
axes <- names(dim_names)
dim <- list_sizes_unnamed(dim_names)
size_dim <- prod(dim)
size_dim <- prod(list_sizes_unnamed(dimnames(x)))

meas_names <- colnames(x)
size_meas <- big_mark(vec_size(meas_names))

dim_sum <- c(`Dimensions` = commas(paste0(axes, " [", big_mark(dim), "]")))
if (is_ddf_col(x)) {
tbl_sum <- c(`A dibble` = big_mark(size_dim),
dim_sum)
} else {
tbl_sum <- c(`A dibble` = paste(big_mark(size_dim), size_meas,
sep = " x "),
dim_sum,
`Measures` = commas(meas_names))
}

x <- tibble::as_tibble(head_dibble(x,
n = n))
setup <- tbl_format_setup(x,
setup <- tbl_format_setup(tibble::as_tibble(head_dibble(x,
n = n)),
n = n,
...)
setup$tbl_sum <- tbl_sum
setup$tbl_sum <- tbl_sum(x)
rows_total_old <- setup$rows_total
rows_total_new <- size_dim
setup$rows_total <- rows_total_new
Expand Down
14 changes: 14 additions & 0 deletions R/tbl_ddf.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ format.tbl_ddf <- function(x, n = NULL, ...) {
...)
}

#' @export
tbl_sum.tbl_ddf <- function(x) {
dim_names <- dimnames(x)
dim <- list_sizes_unnamed(dim_names)
size_dim <- prod(dim)
meas_names <- colnames(x)
size_meas <- big_mark(vec_size(meas_names))

c(`A dibble` = paste(big_mark(size_dim), size_meas,
sep = " x "),
`Dimensions` = commas(paste0(names(dim_names), " [", big_mark(dim), "]")),
`Measures` = commas(meas_names))
}

#' @export
tbl_format_setup.tbl_ddf <- function(x, width = NULL, ..., n = NULL, max_extra_cols = NULL, max_footer_lines = NULL, focus = NULL) {
tbl_format_setup_dibble(x,
Expand Down

0 comments on commit 1abe887

Please sign in to comment.