Skip to content

Commit

Permalink
Update print.egor() to rely on 'pillar'
Browse files Browse the repository at this point in the history
Printing 'egor' objects now simply calls pillar:::print.tbl_df() for
each of the elements of the objects. In order to skip the "header" which
we print in our own way (data level plus the "active" adjective if
applicable) it is necessary to add the method for tbl_sum() as printing
of tibbles cannot be controled with arguments nor options.

Package 'pillar' is now imported.

Upgrade RoxygenNote in DESCRIPTION.

References #80.
  • Loading branch information
mbojan committed Dec 4, 2022
1 parent 03b51ae commit e70abcb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Imports:
methods,
utils,
purrr,
rlang
rlang,
pillar
Suggests:
knitr,
testthat (>= 2.1.0),
Expand All @@ -52,7 +53,7 @@ Suggests:
network,
haven
VignetteBuilder: knitr
RoxygenNote: 7.1.2
RoxygenNote: 7.2.2
Roxygen: list(markdown = TRUE)
LazyData: true
Encoding: UTF-8
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ S3method(subset,egor)
S3method(summarise,egor)
S3method(summarize,egor)
S3method(summary,egor)
S3method(tbl_sum,egor_tibble)
S3method(tbl_vars,egor)
S3method(transmute,egor)
S3method(ungroup,egor)
Expand Down
74 changes: 43 additions & 31 deletions R/egor.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,36 +310,48 @@ print.egor <- function(x,
data_levels <- x
}

purrr::pwalk(list(data_levels,
names(data_levels),
active_lgl),
function(data_level, level_name, active) {
design <- NULL
if ("tbl_svy" %in% class(data_level)) {
data_level <- data_level$variables
design <- " with survey design"
}

if (active) tcm <- tibble::trunc_mat(data_level, n = min(n.active, nrow(data_level)))
else tcm <- tibble::trunc_mat(data_level, n = min(n.inactive, nrow(data_level)))

if (is_grouped_df(data_level))
tcm$summary <- paste(tcm$summary, collapse = " ")

if (active)
cat(paste0(
"# ",
toupper(level_name),
" data",
design ,
" (\033[32mactive\033[39m): ",
tcm$summary[1],
"\n"
))
else
cat(paste0("# ", toupper(level_name), " data", design , ": ", tcm$summary[1], "\n"))

print(tcm$mcf)
})
purrr::pwalk(
list(data_levels, names(data_levels), active_lgl),
function(data_level, level_name, active) {
design <- NULL
if ("tbl_svy" %in% class(data_level)) {
data_level <- data_level$variables
design <- " with survey design"
}
summary_row <- pillar::tbl_sum(data_level)

if (is_grouped_df(data_level)) {
# MB: not tested properly
summary_row <- paste(summary_row, collapse = " ")
}

if (active)
cat(paste0(
"# ",
toupper(level_name),
" data",
design ,
" (\033[32mactive\033[39m): ",
summary_row,
"\n"
))
else
cat(paste0("# ", toupper(level_name), " data", design , ": ", summary_row, "\n"))

print(
structure(data_level, class=c("egor_tibble", class(data_level))),
n = if(active) {
min(n.active, nrow(data_level))
} else {
min(n.inactive, nrow(data_level))
}
)
})
invisible(x)
}

#MB: Seemingly the only way to control printing of tibbles is to define a new
#inherting S3 class... ' @export
tbl_sum.egor_tibble <- function(x) {
NULL
}

0 comments on commit e70abcb

Please sign in to comment.