Skip to content

Commit

Permalink
Two-step computation of setup
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Nov 22, 2024
1 parent 4be51a7 commit 15f4c98
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 30 deletions.
51 changes: 37 additions & 14 deletions R/tbl-format-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
#' This argument is mandatory for all implementations of this method.
#' @param ...
#' Extra arguments to [print.tbl()] or [format.tbl()].
#' @param setup
#' This method is first called with `setup = NULL` .
#' If the method _evaluates_ this argument, the return value
#' will only be used in a call to [tbl_format_header()],
#' and after that, a second call to this method will be made
#' with the return value of the first call as `setup`.
#' This allows displaying the header before starting the computation
#' required for the body and footer.
#' @param n
#' Actual number of rows to print.
#' No [options][pillar_options] should be considered
Expand All @@ -67,6 +75,7 @@ tbl_format_setup <- function(
x,
width = NULL,
...,
setup = list(tbl_sum = tbl_sum(x)),
n = NULL,
max_extra_cols = NULL,
max_footer_lines = NULL,
Expand All @@ -87,6 +96,7 @@ tbl_format_setup <- function(
x,
width,
...,
setup = setup,
n = n,
max_extra_cols = max_extra_cols,
max_footer_lines = max_footer_lines,
Expand All @@ -107,10 +117,26 @@ tbl_format_setup_dispatch <- function(x, width, ..., n, max_extra_cols, max_foot
#'
#' @rdname tbl_format_setup
#' @export
tbl_format_setup.tbl <- function(x, width, ...,
n, max_extra_cols, max_footer_lines, focus) {
tbl_format_setup.tbl <- function(
x,
width,
...,
setup,
n,
max_extra_cols,
max_footer_lines,
focus
) {
"!!!!DEBUG tbl_format_setup.tbl()"

if (is.null(setup)) {
# Header with early exit
tbl_sum <- tbl_sum(x)
return(new_tbl_format_setup(width, tbl_sum))
} else {
tbl_sum <- setup$tbl_sum
}

# Number of rows
rows <- tbl_nrow(x)

Expand Down Expand Up @@ -140,9 +166,6 @@ tbl_format_setup.tbl <- function(x, width, ...,
rows_missing <- 0L
}

# Header
tbl_sum <- tbl_sum(x)

# Body
rownames(df) <- NULL

Expand Down Expand Up @@ -233,17 +256,17 @@ tbl_nrow.tbl <- function(x, ...) {
#'
#' @keywords internal
new_tbl_format_setup <- function(
x,
df,
width,
tbl_sum,
body,
rows_missing,
rows_total,
extra_cols,
extra_cols_total,
max_footer_lines,
abbrev_cols
x = NULL,
df = NULL,
body = NULL,
rows_missing = NULL,
rows_total = NULL,
extra_cols = NULL,
extra_cols_total = NULL,
max_footer_lines = NULL,
abbrev_cols = NULL
) {
trunc_info <- list(
x = x,
Expand Down
26 changes: 24 additions & 2 deletions R/tbl-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,37 @@ format_tbl <- function(
force(x)
num_colors(forget = TRUE)

setup <- tbl_format_setup(x,
width = width, ...,
setup_used <- FALSE

setup <- tbl_format_setup(
x,
width = width,
...,
setup = {
setup_used <- TRUE
NULL
},
n = n,
max_extra_cols = max_extra_cols,
max_footer_lines = max_footer_lines,
focus = attr(x, "pillar_focus")
)

header <- transform(tbl_format_header(x, setup))

if (setup_used) {
setup <- tbl_format_setup(
x,
width = width,
...,
setup = setup,
n = n,
max_extra_cols = max_extra_cols,
max_footer_lines = max_footer_lines,
focus = attr(x, "pillar_focus")
)
}

body <- transform(tbl_format_body(x, setup))
footer <- transform(tbl_format_footer(x, setup))
c(header, body, footer)
Expand Down
26 changes: 13 additions & 13 deletions man/new_tbl_format_setup.Rd

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

20 changes: 19 additions & 1 deletion man/tbl_format_setup.Rd

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

0 comments on commit 15f4c98

Please sign in to comment.