diff --git a/R/options.R b/R/options.R index df4775613..bac6e24c8 100644 --- a/R/options.R +++ b/R/options.R @@ -186,10 +186,17 @@ get_n_print <- function(n, rows) { } get_max_extra_cols <- function(max_extra_cols) { - # FIXME: Deprecate if (!is.null(max_extra_cols) && max_extra_cols >= 0) { return(max_extra_cols) } get_pillar_option_max_extra_cols() } + +get_max_footer_lines <- function(max_footer_lines) { + if (!is.null(max_footer_lines) && max_footer_lines >= 0) { + return(max_footer_lines) + } + + get_pillar_option_max_footer_lines() +} diff --git a/R/tbl-format-footer.R b/R/tbl-format-footer.R index 1a6db9887..63961fa0b 100644 --- a/R/tbl-format-footer.R +++ b/R/tbl-format-footer.R @@ -118,7 +118,7 @@ wrap_footer <- function(footer, setup) { tier_widths <- get_footer_tier_widths( footer, max_extent, - get_pillar_option_max_footer_lines() + setup$max_footer_lines ) # show optuput even if too wide diff --git a/R/tbl-format-setup.R b/R/tbl-format-setup.R index 12107b640..9be0f80e6 100644 --- a/R/tbl-format-setup.R +++ b/R/tbl-format-setup.R @@ -50,6 +50,10 @@ #' if the width is too small for the entire tibble. #' No [options][pillar_options] should be considered #' by implementations of this method. +#' @param max_footer_lines +#' Maximum number of lines for the footer. +#' No [options][pillar_options] should be considered +#' by implementations of this method. #' #' @return #' An object that can be passed as `setup` argument to @@ -58,7 +62,8 @@ #' @examplesIf rlang::is_installed("palmerpenguins") #' tbl_format_setup(palmerpenguins::penguins) tbl_format_setup <- function(x, width = NULL, ..., - n = NULL, max_extra_cols = NULL) { + n = NULL, max_extra_cols = NULL, + max_footer_lines = NULL) { "!!!!DEBUG tbl_format_setup()" width <- get_width_print(width) @@ -66,15 +71,19 @@ tbl_format_setup <- function(x, width = NULL, ..., n <- get_n_print(n, nrow(x)) max_extra_cols <- get_max_extra_cols(max_extra_cols) + max_footer_lines <- get_max_footer_lines(max_footer_lines) # Calls UseMethod("tbl_format_setup"), # allows using default values in S3 dispatch - out <- tbl_format_setup_(x, width, ..., n = n, max_extra_cols = max_extra_cols) + out <- tbl_format_setup_( + x, width, ..., + n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines + ) return(out) UseMethod("tbl_format_setup") } -tbl_format_setup_ <- function(x, width, ..., n, max_extra_cols) { +tbl_format_setup_ <- function(x, width, ..., n, max_extra_cols, max_footer_lines) { UseMethod("tbl_format_setup") } @@ -86,7 +95,7 @@ tbl_format_setup_ <- function(x, width, ..., n, max_extra_cols) { #' @rdname tbl_format_setup #' @export tbl_format_setup.tbl <- function(x, width, ..., - n, max_extra_cols) { + n, max_extra_cols, max_footer_lines) { "!!!!DEBUG tbl_format_setup.tbl()" # Number of rows @@ -150,7 +159,8 @@ tbl_format_setup.tbl <- function(x, width, ..., rows_missing = rows_missing, rows_total = rows, extra_cols = extra_cols, - extra_cols_total = extra_cols_total + extra_cols_total = extra_cols_total, + max_footer_lines = max_footer_lines ) } @@ -179,11 +189,13 @@ tbl_format_setup.tbl <- function(x, width, ..., #' as a character vector of formatted column names and types. #' @param extra_cols_total The total number of columns, may be larger than #' `length(extra_cols)`. +#' @param max_footer_lines The maximum number of lines in the footer. #' #' @keywords internal new_tbl_format_setup <- function(x, df, width, tbl_sum, body, rows_missing, rows_total, - extra_cols, extra_cols_total) { + extra_cols, extra_cols_total, + max_footer_lines) { trunc_info <- list( x = x, df = df, @@ -193,7 +205,8 @@ new_tbl_format_setup <- function(x, df, width, tbl_sum, body, rows_missing = rows_missing, rows_total = rows_total, extra_cols = extra_cols, - extra_cols_total = extra_cols_total + extra_cols_total = extra_cols_total, + max_footer_lines = max_footer_lines ) structure(trunc_info, class = "pillar_tbl_format_setup") diff --git a/R/tbl-format.R b/R/tbl-format.R index e4ccccb41..9364a301f 100644 --- a/R/tbl-format.R +++ b/R/tbl-format.R @@ -30,24 +30,71 @@ #' `print_min` [option][pillar_options]. #' @param width Width of text output to generate. This defaults to `NULL`, which #' means use the `width` [option][pillar_options]. -#' @param n_extra Number of extra columns to print abbreviated information for, +#' @param max_extra_cols Number of extra columns to print abbreviated information for, #' if the width is too small for the entire tibble. If `NULL`, #' the `max_extra_cols` [option][pillar_options] is used. +#' The previously defined `n_extra` argument is soft-deprecated. +#' @param max_footer_lines Maximum number of footer lines. If `NULL`, +#' the `max_footer_lines` [option][pillar_options] is used. #' #' @name format_tbl #' @export #' @examples #' print(vctrs::new_data_frame(list(a = 1), class = "tbl")) -print.tbl <- function(x, width = NULL, ..., n = NULL, n_extra = NULL) { - writeLines(format(x, width = width, ..., n = n, n_extra = n_extra)) +print.tbl <- function(x, width = NULL, ..., n = NULL, max_extra_cols = NULL, + max_footer_lines = NULL) { + print_tbl( + x, width, ..., + n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines + ) +} + +print_tbl <- function(x, width = NULL, ..., + n_extra = NULL, + n = NULL, max_extra_cols = NULL, max_footer_lines = NULL) { + + if (!is.null(n_extra)) { + deprecate_soft( + "1.6.2", "pillar::print(n_extra = )", "pillar::print(max_extra_cols = )", + user_env = caller_env(2) + ) + if (is.null(max_extra_cols)) { + max_extra_cols <- n_extra + } + } + + writeLines(format( + x, width = width, ..., + n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines + )) invisible(x) } #' @export #' @rdname format_tbl -format.tbl <- function(x, width = NULL, ..., n = NULL, n_extra = NULL) { +format.tbl <- function(x, width = NULL, ..., + n = NULL, max_extra_cols = NULL, max_footer_lines = NULL) { + format_tbl( + x, width, ..., + n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines + ) +} + +format_tbl <- function(x, width = NULL, ..., + n_extra = NULL, + n = NULL, max_extra_cols = NULL, max_footer_lines = NULL) { check_dots_empty(action = signal) + if (!is.null(n_extra)) { + deprecate_soft( + "1.6.2", "pillar::format(n_extra = )", "pillar::format(max_extra_cols = )", + user_env = caller_env(2) + ) + if (is.null(max_extra_cols)) { + max_extra_cols <- n_extra + } + } + # Reset local cache for each new output force(x) num_colors(forget = TRUE) @@ -55,7 +102,8 @@ format.tbl <- function(x, width = NULL, ..., n = NULL, n_extra = NULL) { setup <- tbl_format_setup(x, width = width, ..., n = n, - max_extra_cols = n_extra + max_extra_cols = max_extra_cols, + max_footer_lines = max_footer_lines ) header <- tbl_format_header(x, setup) diff --git a/man/format_tbl.Rd b/man/format_tbl.Rd index 6d170f70d..982805dff 100644 --- a/man/format_tbl.Rd +++ b/man/format_tbl.Rd @@ -6,9 +6,23 @@ \alias{format.tbl} \title{Formatting of tbl objects} \usage{ -\method{print}{tbl}(x, width = NULL, ..., n = NULL, n_extra = NULL) +\method{print}{tbl}( + x, + width = NULL, + ..., + n = NULL, + max_extra_cols = NULL, + max_footer_lines = NULL +) -\method{format}{tbl}(x, width = NULL, ..., n = NULL, n_extra = NULL) +\method{format}{tbl}( + x, + width = NULL, + ..., + n = NULL, + max_extra_cols = NULL, + max_footer_lines = NULL +) } \arguments{ \item{x}{Object to format or print.} @@ -23,9 +37,13 @@ if less than the \code{print_max} \link[=pillar_options]{option}. Otherwise, will print as many rows as specified by the \code{print_min} \link[=pillar_options]{option}.} -\item{n_extra}{Number of extra columns to print abbreviated information for, +\item{max_extra_cols}{Number of extra columns to print abbreviated information for, if the width is too small for the entire tibble. If \code{NULL}, -the \code{max_extra_cols} \link[=pillar_options]{option} is used.} +the \code{max_extra_cols} \link[=pillar_options]{option} is used. +The previously defined \code{n_extra} argument is soft-deprecated.} + +\item{max_footer_lines}{Maximum number of footer lines. If \code{NULL}, +the \code{max_footer_lines} \link[=pillar_options]{option} is used.} } \description{ These functions and methods are responsible for printing objects diff --git a/man/new_tbl_format_setup.Rd b/man/new_tbl_format_setup.Rd index fe6b9b5cb..e34a31aa5 100644 --- a/man/new_tbl_format_setup.Rd +++ b/man/new_tbl_format_setup.Rd @@ -13,7 +13,8 @@ new_tbl_format_setup( rows_missing, rows_total, extra_cols, - extra_cols_total + extra_cols_total, + max_footer_lines ) } \arguments{ @@ -40,6 +41,8 @@ as a character vector of formatted column names and types.} \item{extra_cols_total}{The total number of columns, may be larger than \code{length(extra_cols)}.} + +\item{max_footer_lines}{The maximum number of lines in the footer.} } \description{ The object returned from the default method of \code{\link[=tbl_format_setup]{tbl_format_setup()}} diff --git a/man/tbl_format_setup.Rd b/man/tbl_format_setup.Rd index c810feacd..f60dd8e66 100644 --- a/man/tbl_format_setup.Rd +++ b/man/tbl_format_setup.Rd @@ -5,9 +5,16 @@ \alias{tbl_format_setup.tbl} \title{Set up formatting} \usage{ -tbl_format_setup(x, width = NULL, ..., n = NULL, max_extra_cols = NULL) +tbl_format_setup( + x, + width = NULL, + ..., + n = NULL, + max_extra_cols = NULL, + max_footer_lines = NULL +) -\method{tbl_format_setup}{tbl}(x, width, ..., n, max_extra_cols) +\method{tbl_format_setup}{tbl}(x, width, ..., n, max_extra_cols, max_footer_lines) } \arguments{ \item{x}{An object.} @@ -25,6 +32,10 @@ by implementations of this method.} if the width is too small for the entire tibble. No \link[=pillar_options]{options} should be considered by implementations of this method.} + +\item{max_footer_lines}{Maximum number of lines for the footer. +No \link[=pillar_options]{options} should be considered +by implementations of this method.} } \value{ An object that can be passed as \code{setup} argument to diff --git a/tests/testthat/_snaps/tbl-format-footer.md b/tests/testthat/_snaps/tbl-format-footer.md index 723d04e2f..f5610e50e 100644 --- a/tests/testthat/_snaps/tbl-format-footer.md +++ b/tests/testthat/_snaps/tbl-format-footer.md @@ -68,12 +68,9 @@ # max_footer_lines option Code - new_footer_tbl("") + tbl_format_footer(tbl_format_setup(new_footer_tbl(""))) Output - # A data frame: 1 x 52 - aa ba ab bb ac bc ad bd ae be af bf ag - - 1 1 2 3 4 5 6 7 8 9 10 11 12 13 + # ... with 39 more variables: bg , ah , bh , ai , bi , # aj , bj , ak , bk , al , bl , am , # bm , an , bn , ao , bo , ap , bp , @@ -81,12 +78,9 @@ # bt , au , bu , av , bv , aw , bw , # ax , bx , ay , by , az , bz Code - new_footer_tbl("prefix_") + tbl_format_footer(tbl_format_setup(new_footer_tbl("prefix_"))) Output - # A data frame: 1 x 52 - prefix_aa prefix_ba prefix_ab prefix_bb prefix_ac prefix_bc prefix_ad - - 1 1 2 3 4 5 6 7 + # ... with 45 more variables: prefix_bd , prefix_ae , # prefix_be , prefix_af , prefix_bf , prefix_ag , # prefix_bg , prefix_ah , prefix_bh , prefix_ai , @@ -95,12 +89,9 @@ # prefix_bm , prefix_an , prefix_bn , prefix_ao , # prefix_bo , prefix_ap , prefix_bp , prefix_aq , ... Code - new_footer_tbl("a_very_long_prefix_") + tbl_format_footer(tbl_format_setup(new_footer_tbl("a_very_long_prefix_"))) Output - # A data frame: 1 x 52 - a_very_long_prefix_aa a_very_long_prefix_ba a_very_long_pref~ a_very_long_pre~ - - 1 1 2 3 4 + # ... with 48 more variables: a_very_long_prefix_ac , # a_very_long_prefix_bc , a_very_long_prefix_ad , # a_very_long_prefix_bd , a_very_long_prefix_ae , @@ -109,44 +100,31 @@ # a_very_long_prefix_bg , a_very_long_prefix_ah , # a_very_long_prefix_bh , a_very_long_prefix_ai , ... Code - set_pillar_option_max_footer_lines(3) - new_footer_tbl("") + tbl_format_footer(tbl_format_setup(new_footer_tbl(""), max_footer_lines = 3)) Output - # A data frame: 1 x 52 - aa ba ab bb ac bc ad bd ae be af bf ag - - 1 1 2 3 4 5 6 7 8 9 10 11 12 13 + # ... with 39 more variables: bg , ah , bh , ai , bi , # aj , bj , ak , bk , al , bl , am , # bm , an , bn , ao , bo , ap , bp , ... Code - new_footer_tbl("prefix_") + tbl_format_footer(tbl_format_setup(new_footer_tbl("prefix_"), max_footer_lines = 3)) Output - # A data frame: 1 x 52 - prefix_aa prefix_ba prefix_ab prefix_bb prefix_ac prefix_bc prefix_ad - - 1 1 2 3 4 5 6 7 + # ... with 45 more variables: prefix_bd , prefix_ae , # prefix_be , prefix_af , prefix_bf , prefix_ag , # prefix_bg , prefix_ah , prefix_bh , prefix_ai , ... Code - new_footer_tbl("a_very_long_prefix_") + tbl_format_footer(tbl_format_setup(new_footer_tbl("a_very_long_prefix_"), + max_footer_lines = 3)) Output - # A data frame: 1 x 52 - a_very_long_prefix_aa a_very_long_prefix_ba a_very_long_pref~ a_very_long_pre~ - - 1 1 2 3 4 + # ... with 48 more variables: a_very_long_prefix_ac , # a_very_long_prefix_bc , a_very_long_prefix_ad , # a_very_long_prefix_bd , a_very_long_prefix_ae , ... Code - set_pillar_option_max_footer_lines(Inf) - new_footer_tbl("") + tbl_format_footer(tbl_format_setup(new_footer_tbl(""), max_footer_lines = Inf)) Output - # A data frame: 1 x 52 - aa ba ab bb ac bc ad bd ae be af bf ag - - 1 1 2 3 4 5 6 7 8 9 10 11 12 13 + # ... with 39 more variables: bg , ah , bh , ai , bi , # aj , bj , ak , bk , al , bl , am , # bm , an , bn , ao , bo , ap , bp , @@ -154,12 +132,9 @@ # bt , au , bu , av , bv , aw , bw , # ax , bx , ay , by , az , bz Code - new_footer_tbl("prefix_") + tbl_format_footer(tbl_format_setup(new_footer_tbl("prefix_"), max_footer_lines = Inf)) Output - # A data frame: 1 x 52 - prefix_aa prefix_ba prefix_ab prefix_bb prefix_ac prefix_bc prefix_ad - - 1 1 2 3 4 5 6 7 + # ... with 45 more variables: prefix_bd , prefix_ae , # prefix_be , prefix_af , prefix_bf , prefix_ag , # prefix_bg , prefix_ah , prefix_bh , prefix_ai , @@ -173,12 +148,10 @@ # prefix_bw , prefix_ax , prefix_bx , prefix_ay , # prefix_by , prefix_az , prefix_bz Code - new_footer_tbl("a_very_long_prefix_") + tbl_format_footer(tbl_format_setup(new_footer_tbl("a_very_long_prefix_"), + max_footer_lines = Inf)) Output - # A data frame: 1 x 52 - a_very_long_prefix_aa a_very_long_prefix_ba a_very_long_pref~ a_very_long_pre~ - - 1 1 2 3 4 + # ... with 48 more variables: a_very_long_prefix_ac , # a_very_long_prefix_bc , a_very_long_prefix_ad , # a_very_long_prefix_bd , a_very_long_prefix_ae , diff --git a/tests/testthat/_snaps/tbl-format.md b/tests/testthat/_snaps/tbl-format.md index 89b9dcdde..eee0cc52f 100644 --- a/tests/testthat/_snaps/tbl-format.md +++ b/tests/testthat/_snaps/tbl-format.md @@ -143,6 +143,64 @@ 30 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6 31 15 8 301 335 3.54 3.57 14.6 0 1 5 8 32 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2 + Code + print(as_tbl(mtcars), width = 40, n_extra = 1) + Warning + The `n_extra` argument of `print()` is deprecated as of pillar 1.6.2. + Please use the `max_extra_cols` argument instead. + Output + # A data frame: 32 x 11 + mpg cyl disp hp drat wt + * + 1 21 6 160 110 3.9 2.62 + 2 21 6 160 110 3.9 2.88 + 3 22.8 4 108 93 3.85 2.32 + 4 21.4 6 258 110 3.08 3.22 + 5 18.7 8 360 175 3.15 3.44 + 6 18.1 6 225 105 2.76 3.46 + 7 14.3 8 360 245 3.21 3.57 + 8 24.4 4 147. 62 3.69 3.19 + 9 22.8 4 141. 95 3.92 3.15 + 10 19.2 6 168. 123 3.92 3.44 + # ... with 22 more rows, and 5 more + # variable: qsec , ... + Code + print(as_tbl(mtcars), width = 40, max_extra_cols = 1) + Output + # A data frame: 32 x 11 + mpg cyl disp hp drat wt + * + 1 21 6 160 110 3.9 2.62 + 2 21 6 160 110 3.9 2.88 + 3 22.8 4 108 93 3.85 2.32 + 4 21.4 6 258 110 3.08 3.22 + 5 18.7 8 360 175 3.15 3.44 + 6 18.1 6 225 105 2.76 3.46 + 7 14.3 8 360 245 3.21 3.57 + 8 24.4 4 147. 62 3.69 3.19 + 9 22.8 4 141. 95 3.92 3.15 + 10 19.2 6 168. 123 3.92 3.44 + # ... with 22 more rows, and 5 more + # variable: qsec , ... + Code + print(as_tbl(mtcars), width = 30, max_footer_lines = 3) + Output + # A data frame: 32 x 11 + mpg cyl disp hp + * + 1 21 6 160 110 + 2 21 6 160 110 + 3 22.8 4 108 93 + 4 21.4 6 258 110 + 5 18.7 8 360 175 + 6 18.1 6 225 105 + 7 14.3 8 360 245 + 8 24.4 4 147. 62 + 9 22.8 4 141. 95 + 10 19.2 6 168. 123 + # ... with 22 more rows, and + # 7 more variables: + # drat , wt , ... Code rlang::with_options(tibble.print_min = 5, as_tbl(mtcars)) Output diff --git a/tests/testthat/test-tbl-format-footer.R b/tests/testthat/test-tbl-format-footer.R index 47505c08b..3560be802 100644 --- a/tests/testthat/test-tbl-format-footer.R +++ b/tests/testthat/test-tbl-format-footer.R @@ -33,19 +33,45 @@ test_that("max_footer_lines option", { new_tbl(x) } + expect_identical( + local({ + local_pillar_option_max_footer_lines(3) + tbl_format_footer(tbl_format_setup(new_footer_tbl(""))) + }), + tbl_format_footer( + tbl_format_setup(new_footer_tbl(""), max_footer_lines = 3) + ) + ) + expect_snapshot({ - new_footer_tbl("") - new_footer_tbl("prefix_") - new_footer_tbl("a_very_long_prefix_") - - set_pillar_option_max_footer_lines(3) - new_footer_tbl("") - new_footer_tbl("prefix_") - new_footer_tbl("a_very_long_prefix_") - - set_pillar_option_max_footer_lines(Inf) - new_footer_tbl("") - new_footer_tbl("prefix_") - new_footer_tbl("a_very_long_prefix_") + tbl_format_footer( + tbl_format_setup(new_footer_tbl("")) + ) + tbl_format_footer( + tbl_format_setup(new_footer_tbl("prefix_")) + ) + tbl_format_footer( + tbl_format_setup(new_footer_tbl("a_very_long_prefix_")) + ) + + tbl_format_footer( + tbl_format_setup(new_footer_tbl(""), max_footer_lines = 3) + ) + tbl_format_footer( + tbl_format_setup(new_footer_tbl("prefix_"), max_footer_lines = 3) + ) + tbl_format_footer( + tbl_format_setup(new_footer_tbl("a_very_long_prefix_"), max_footer_lines = 3) + ) + + tbl_format_footer( + tbl_format_setup(new_footer_tbl(""), max_footer_lines = Inf) + ) + tbl_format_footer( + tbl_format_setup(new_footer_tbl("prefix_"), max_footer_lines = Inf) + ) + tbl_format_footer( + tbl_format_setup(new_footer_tbl("a_very_long_prefix_"), max_footer_lines = Inf) + ) }) }) diff --git a/tests/testthat/test-tbl-format.R b/tests/testthat/test-tbl-format.R index ac549e691..61d80b44c 100644 --- a/tests/testthat/test-tbl-format.R +++ b/tests/testthat/test-tbl-format.R @@ -24,6 +24,12 @@ test_that("print() output", { print(as_tbl(mtcars), n = 100) + print(as_tbl(mtcars), width = 40, n_extra = 1) + + print(as_tbl(mtcars), width = 40, max_extra_cols = 1) + + print(as_tbl(mtcars), width = 30, max_footer_lines = 3) + rlang::with_options( tibble.print_min = 5, as_tbl(mtcars)