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

Show list of abbreviated columns #483

Merged
merged 11 commits into from
Apr 23, 2022
30 changes: 22 additions & 8 deletions R/ctl_colonnade.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ ctl_colonnade <- function(x, has_row_id = TRUE, width = NULL,
last_abbrev_title <<- title
}

on_get_n_abbrev_cols <- function() {
length(abbrev_cols)
}

cb <- new_emit_tiers_callbacks(
controller, rowid, rowid_width, has_star,
on_tier, on_extra_cols, on_abbrev_col
on_tier, on_extra_cols, on_abbrev_col, on_get_n_abbrev_cols
)

# Side effect: populate formatted_tiers, extra_cols, and abbrev_cols
Expand All @@ -96,15 +100,16 @@ ctl_colonnade <- function(x, has_row_id = TRUE, width = NULL,
}

new_emit_tiers_callbacks <- function(controller, rowid, rowid_width, has_star,
on_tier, on_extra_cols, on_abbrev_col) {
on_tier, on_extra_cols, on_abbrev_col, on_get_n_abbrev_cols) {
list(
controller = controller,
rowid = rowid,
rowid_width = rowid_width,
has_star = has_star,
on_tier = on_tier,
on_extra_cols = on_extra_cols,
on_abbrev_col = on_abbrev_col
on_abbrev_col = on_abbrev_col,
on_get_n_abbrev_cols = on_get_n_abbrev_cols
)
}

Expand Down Expand Up @@ -159,7 +164,8 @@ do_emit_tiers <- function(x, tier_widths, n_focus, cb, focus) {
on_pillar = on_pillar,
on_top_level_pillar = function(...) {},
on_extra_cols = on_extra_cols,
on_abbrev_col = cb$on_abbrev_col
on_abbrev_col = cb$on_abbrev_col,
on_get_n_abbrev_cols = cb$on_get_n_abbrev_cols
)

# Side effect: populate `extra_cols`,
Expand All @@ -175,15 +181,17 @@ new_emit_pillars_callbacks <- function(controller,
on_pillar,
on_top_level_pillar,
on_extra_cols,
on_abbrev_col) {
on_abbrev_col,
on_get_n_abbrev_cols) {
list(
controller = controller,
on_start_tier = on_start_tier,
on_end_tier = on_end_tier,
on_pillar = on_pillar,
on_top_level_pillar = on_top_level_pillar,
on_extra_cols = on_extra_cols,
on_abbrev_col = on_abbrev_col
on_abbrev_col = on_abbrev_col,
on_get_n_abbrev_cols = on_get_n_abbrev_cols
)
}

Expand Down Expand Up @@ -239,7 +247,8 @@ do_emit_focus_pillars <- function(x, tier_widths, cb, focus) {
on_pillar = on_focus_pillar,
on_top_level_pillar = on_focus_top_level_pillar,
on_extra_cols = on_focus_extra_cols,
on_abbrev_col = cb$on_abbrev_col
on_abbrev_col = cb$on_abbrev_col,
on_get_n_abbrev_cols = cb$on_get_n_abbrev_cols
)

# Side effect: populates focus_formatted_list and focus_extra_cols
Expand Down Expand Up @@ -384,7 +393,12 @@ do_emit_pillars <- function(x, tier_widths, cb, title = NULL, first_pillar = NUL

title_width <- get_width(pillar[["title"]]) %||% 0L

formatted <- pillar_format_parts_2(pillar, max(tier_widths), is_focus)
formatted <- pillar_format_parts_2(
pillar,
max(tier_widths),
is_focus,
cb$on_get_n_abbrev_cols() + 1L
)
true_width <- formatted$max_extent
stopifnot(true_width <= max(tier_widths))

Expand Down
5 changes: 3 additions & 2 deletions R/ctl_pillar_component.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ pillar_get_min_width <- function(x) {
as.integer(max(map_int(x, get_min_width)))
}

pillar_format_parts_2 <- function(x, width, is_focus = FALSE) {
pillar_format_parts_2 <- function(x, width, is_focus = FALSE, footnote_idx = 1L) {
formatted <- map(x, function(.x) format(
.x[[1L]],
width = min(width, get_width(.x)),
is_focus = is_focus
is_focus = is_focus,
footnote_idx = footnote_idx
))

align <- attr(formatted[["data"]], "align", exact = TRUE) %||% "left"
Expand Down
23 changes: 21 additions & 2 deletions R/tbl-format-footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ tbl_format_footer.tbl <- function(x, setup, ...) {

format_footer <- function(x, setup) {
extra_rows <- format_footer_extra_rows(x, setup)
abbrev_cols <- format_footer_abbrev_cols(x, setup)
extra_cols <- format_footer_extra_cols(x, setup)

footer <- compact(list(extra_rows, extra_cols))
footer <- compact(list(extra_rows, abbrev_cols, extra_cols))
if (length(footer) == 0) {
return(character())
}

if (length(footer) > 1) {
footer_len <- length(footer)
idx_all_but_last <- seq.int(footer_len - 1)
idx_all_but_last <- seq2(1, footer_len - 1)
footer[idx_all_but_last] <- map(footer[idx_all_but_last], function(x) {
x[[length(x)]] <- paste0(x[[length(x)]], ",")
x
Expand Down Expand Up @@ -83,6 +84,24 @@ format_footer_extra_rows <- function(x, setup) {
}
}

format_footer_abbrev_cols <- function(x, setup) {
abbrev_cols <- setup$abbrev_cols
abbrev_cols_total <- length(abbrev_cols)
if (abbrev_cols_total == 0) {
return(NULL)
}

abbrev_cols <- paste0(map_chr(seq_along(abbrev_cols), superdigit_pre), abbrev_cols)
idx_all_but_last <- seq_len(abbrev_cols_total - 1)
abbrev_cols[idx_all_but_last] <- paste0(abbrev_cols[idx_all_but_last], ",")

c(
"abbreviated", "variable",
pluralise("name(s)", abbrev_cols),
abbrev_cols
)
}

format_footer_extra_cols <- function(x, setup) {
extra_cols <- setup$extra_cols
if (length(extra_cols) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion R/tick.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
format_title <- function(x, width, footnote = FALSE) {
format_title <- function(x, width, footnote = TRUE) {
align(str_trunc(x, width, if (footnote) "untick_footnote" else "untick"))
}

Expand Down
8 changes: 6 additions & 2 deletions R/title.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ get_min_title_width <- function(width) {
}

#' @export
format.pillar_title <- function(x, width = NULL, ...) {
format.pillar_title <- function(x, width = NULL, ..., footnote_idx = 1L) {
title <- x[[1]]
if (is.null(title)) {
return(character())
Expand All @@ -61,6 +61,10 @@ format.pillar_title <- function(x, width = NULL, ...) {
width <- get_width(x)
}

title <- format_title(title, width)
footnote <- (footnote_idx > 0L)
title <- format_title(title, width, footnote)
if (footnote) {
title <- gsub("\u02df$", superdigit(footnote_idx), title)
}
style_title(title)
}
22 changes: 22 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,25 @@ vec_lag <- function(x, default = vec_slice(x, NA_integer_)) {
}
vec_c(default, vec_slice(x, -n))
}

super <- c(
"\u00b9", "\u00b2", "\u00b3", "\u2074",
"\u2075", "\u2076", "\u2077", "\u2078", "\u2079",
"\u02df"
)

superdigit <- function(x) {
if (cli::is_utf8_output()) {
super[[min(x, 10)]]
} else {
if (x >= 10) "*" else as.character(x)
}
}

superdigit_pre <- function(x) {
if (cli::is_utf8_output()) {
paste0(superdigit(x), " ")
} else {
paste0(superdigit(x), ": ")
}
}
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ansi/ctl_colonnade.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
ctl_colonnade(list(a_very_long_column_name = 0), width = 20)
Output
$body
a_very_long_colum~
a_very_long_colu~1
<dbl>
1 0

Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/_snaps/format_multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
colu~
col~1
<dbl>
1 1.23
2 2.23
Expand All @@ -56,7 +56,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
colum~
colu~1
<dbl>
1 1.23
2 2.23
Expand All @@ -68,7 +68,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column~
colum~1
<dbl>
1 1.23
2 2.23
Expand All @@ -80,7 +80,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_~
column~1
<dbl>
1 1.23
2 2.23
Expand All @@ -92,7 +92,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_z~
column_~1
<dbl>
1 1.23
2 2.23
Expand All @@ -104,7 +104,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_ze~
column_z~1
<dbl>
1 1.23
2 2.23
Expand All @@ -116,7 +116,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_zer~
column_ze~1
<dbl>
1 1.23
2 2.23
Expand All @@ -128,7 +128,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_zero~
column_zer~1
<dbl>
1 1.23
2 2.23
Expand All @@ -140,7 +140,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_zero_~
column_zero~1
<dbl>
1 1.23
2 2.23
Expand All @@ -152,7 +152,7 @@
`colonnade()` was deprecated in pillar 1.7.0.
Please use `tbl_format_setup()` instead.
Output
column_zero_o~
column_zero_~1
<dbl>
1 1.23
2 2.23
Expand Down
71 changes: 36 additions & 35 deletions tests/testthat/_snaps/tbl-format-footer.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@
tbl_format_footer(tbl_format_setup(new_footer_tbl("a_very_long_prefix_")))
Output
<tbl_format_footer(setup)>
# ... with 48 more variables: a_very_long_prefix_ac <int>,
# a_very_long_prefix_bc <int>, a_very_long_prefix_ad <int>,
# a_very_long_prefix_bd <int>, a_very_long_prefix_ae <int>,
# a_very_long_prefix_be <int>, a_very_long_prefix_af <int>,
# a_very_long_prefix_bf <int>, a_very_long_prefix_ag <int>,
# a_very_long_prefix_bg <int>, a_very_long_prefix_ah <int>,
# a_very_long_prefix_bh <int>, a_very_long_prefix_ai <int>, ...
# ... with abbreviated variable names 1: a_very_long_prefix_ab,
# 2: a_very_long_prefix_bb, and 48 more variables:
# a_very_long_prefix_ac <int>, a_very_long_prefix_bc <int>,
# a_very_long_prefix_ad <int>, a_very_long_prefix_bd <int>,
# a_very_long_prefix_ae <int>, a_very_long_prefix_be <int>,
# a_very_long_prefix_af <int>, a_very_long_prefix_bf <int>,
# a_very_long_prefix_ag <int>, a_very_long_prefix_bg <int>, ...
Code
tbl_format_footer(tbl_format_setup(new_footer_tbl(""), max_footer_lines = 3))
Output
Expand All @@ -98,9 +98,9 @@
max_footer_lines = 3))
Output
<tbl_format_footer(setup)>
# ... with 48 more variables: a_very_long_prefix_ac <int>,
# a_very_long_prefix_bc <int>, a_very_long_prefix_ad <int>,
# a_very_long_prefix_bd <int>, a_very_long_prefix_ae <int>, ...
# ... with abbreviated variable names 1: a_very_long_prefix_ab,
# 2: a_very_long_prefix_bb, and 48 more variables:
# a_very_long_prefix_ac <int>, a_very_long_prefix_bc <int>, ...
Code
tbl_format_footer(tbl_format_setup(new_footer_tbl(""), max_footer_lines = Inf))
Output
Expand Down Expand Up @@ -132,29 +132,30 @@
max_footer_lines = Inf))
Output
<tbl_format_footer(setup)>
# ... with 48 more variables: a_very_long_prefix_ac <int>,
# a_very_long_prefix_bc <int>, a_very_long_prefix_ad <int>,
# a_very_long_prefix_bd <int>, a_very_long_prefix_ae <int>,
# a_very_long_prefix_be <int>, a_very_long_prefix_af <int>,
# a_very_long_prefix_bf <int>, a_very_long_prefix_ag <int>,
# a_very_long_prefix_bg <int>, a_very_long_prefix_ah <int>,
# a_very_long_prefix_bh <int>, a_very_long_prefix_ai <int>,
# a_very_long_prefix_bi <int>, a_very_long_prefix_aj <int>,
# a_very_long_prefix_bj <int>, a_very_long_prefix_ak <int>,
# a_very_long_prefix_bk <int>, a_very_long_prefix_al <int>,
# a_very_long_prefix_bl <int>, a_very_long_prefix_am <int>,
# a_very_long_prefix_bm <int>, a_very_long_prefix_an <int>,
# a_very_long_prefix_bn <int>, a_very_long_prefix_ao <int>,
# a_very_long_prefix_bo <int>, a_very_long_prefix_ap <int>,
# a_very_long_prefix_bp <int>, a_very_long_prefix_aq <int>,
# a_very_long_prefix_bq <int>, a_very_long_prefix_ar <int>,
# a_very_long_prefix_br <int>, a_very_long_prefix_as <int>,
# a_very_long_prefix_bs <int>, a_very_long_prefix_at <int>,
# a_very_long_prefix_bt <int>, a_very_long_prefix_au <int>,
# a_very_long_prefix_bu <int>, a_very_long_prefix_av <int>,
# a_very_long_prefix_bv <int>, a_very_long_prefix_aw <int>,
# a_very_long_prefix_bw <int>, a_very_long_prefix_ax <int>,
# a_very_long_prefix_bx <int>, a_very_long_prefix_ay <int>,
# a_very_long_prefix_by <int>, a_very_long_prefix_az <int>,
# a_very_long_prefix_bz <int>
# ... with abbreviated variable names 1: a_very_long_prefix_ab,
# 2: a_very_long_prefix_bb, and 48 more variables:
# a_very_long_prefix_ac <int>, a_very_long_prefix_bc <int>,
# a_very_long_prefix_ad <int>, a_very_long_prefix_bd <int>,
# a_very_long_prefix_ae <int>, a_very_long_prefix_be <int>,
# a_very_long_prefix_af <int>, a_very_long_prefix_bf <int>,
# a_very_long_prefix_ag <int>, a_very_long_prefix_bg <int>,
# a_very_long_prefix_ah <int>, a_very_long_prefix_bh <int>,
# a_very_long_prefix_ai <int>, a_very_long_prefix_bi <int>,
# a_very_long_prefix_aj <int>, a_very_long_prefix_bj <int>,
# a_very_long_prefix_ak <int>, a_very_long_prefix_bk <int>,
# a_very_long_prefix_al <int>, a_very_long_prefix_bl <int>,
# a_very_long_prefix_am <int>, a_very_long_prefix_bm <int>,
# a_very_long_prefix_an <int>, a_very_long_prefix_bn <int>,
# a_very_long_prefix_ao <int>, a_very_long_prefix_bo <int>,
# a_very_long_prefix_ap <int>, a_very_long_prefix_bp <int>,
# a_very_long_prefix_aq <int>, a_very_long_prefix_bq <int>,
# a_very_long_prefix_ar <int>, a_very_long_prefix_br <int>,
# a_very_long_prefix_as <int>, a_very_long_prefix_bs <int>,
# a_very_long_prefix_at <int>, a_very_long_prefix_bt <int>,
# a_very_long_prefix_au <int>, a_very_long_prefix_bu <int>,
# a_very_long_prefix_av <int>, a_very_long_prefix_bv <int>,
# a_very_long_prefix_aw <int>, a_very_long_prefix_bw <int>,
# a_very_long_prefix_ax <int>, a_very_long_prefix_bx <int>,
# a_very_long_prefix_ay <int>, a_very_long_prefix_by <int>,
# a_very_long_prefix_az <int>, a_very_long_prefix_bz <int>

Loading