Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak format() and print() #360

Merged
merged 6 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion R/tbl-format-footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 20 additions & 7 deletions R/tbl-format-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,23 +62,28 @@
#' @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)

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")
}

Expand All @@ -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
Expand Down Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
58 changes: 53 additions & 5 deletions R/tbl-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,80 @@
#' `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)

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)
Expand Down
26 changes: 22 additions & 4 deletions man/format_tbl.Rd

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

5 changes: 4 additions & 1 deletion man/new_tbl_format_setup.Rd

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

15 changes: 13 additions & 2 deletions man/tbl_format_setup.Rd

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

Loading