Skip to content

Commit

Permalink
Merge pull request #549 from r-lib/f-focus-attr
Browse files Browse the repository at this point in the history
feat: Pick up `"pillar_focus"` attribute on printing to define focus columns
  • Loading branch information
krlmlr authored Jun 30, 2022
2 parents 1cffc79 + 4d97ab9 commit cf5556d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions R/tbl-format-header.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ tbl_format_header <- function(x, setup, ...) {
#' @export
tbl_format_header.tbl <- function(x, setup, ...) {
named_header <- setup$tbl_sum
focus <- attr(x, "pillar_focus")
if (!is.null(focus)) {
named_header <- c(named_header, "Focus columns" = collapse(tick_if_needed(focus)))
}

if (all(names2(named_header) == "")) {
header <- named_header
Expand Down
3 changes: 2 additions & 1 deletion R/tbl-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ format_tbl <- function(x, width = NULL, ...,
width = width, ...,
n = n,
max_extra_cols = max_extra_cols,
max_footer_lines = max_footer_lines
max_footer_lines = max_footer_lines,
focus = attr(x, "pillar_focus")
)

header <- tbl_format_header(x, setup)
Expand Down
3 changes: 1 addition & 2 deletions R/tbl.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
new_tbl <- function(x = list(), n = NULL, ..., class = NULL) {
check_dots_empty()
new_data_frame(x, n, class = c(class, "tbl"))
new_data_frame(x, n, class = c(class, "tbl"), ...)
}

as_tbl <- function(x, ...) {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/tbl-format-header.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@
<tbl_format_header(setup)>
# A data frame: 0 x 0
# foo: bar
Code
# Focus columns
tbl_format_header(tbl_format_setup(new_tbl(trees, pillar_focus = "Volume"),
width = 30))
Output
<tbl_format_header(setup)>
# A data frame: 31 x 3
# Focus columns: Volume

22 changes: 22 additions & 0 deletions tests/testthat/_snaps/tbl-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,26 @@
10 19.2 6 168. 123 3.92 3.44
# ... with 22 more rows, and 5 more
# variables: qsec <dbl>, ...
Code
print(tbl_format_setup(new_tbl(trees, pillar_focus = "Volume"), width = 30))
Output
<pillar_tbl_format_setup>
<tbl_format_header(setup)>
# A data frame: 31 x 3
# Focus columns: Volume
<tbl_format_body(setup)>
Girth Height Volume
<dbl> <dbl> <dbl>
1 8.3 70 10.3
2 8.6 65 10.3
3 8.8 63 10.2
4 10.5 72 16.4
5 10.7 81 18.8
6 10.8 83 19.7
7 11 66 15.6
8 11 75 18.2
9 11.1 80 22.6
10 11.2 75 19.9
<tbl_format_footer(setup)>
# ... with 21 more rows

3 changes: 3 additions & 0 deletions tests/testthat/test-tbl-format-header.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ test_that("tbl_format_header() results", {

"Custom tbl_sum()"
tbl_format_header(tbl_format_setup(new_foo_tbl(), width = 30))

"Focus columns"
tbl_format_header(tbl_format_setup(new_tbl(trees, pillar_focus = "Volume"), width = 30))
})
})
2 changes: 2 additions & 0 deletions tests/testthat/test-tbl-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ test_that("print() output", {

expect_snapshot({
print(as_tbl(mtcars), width = 40, n_extra = 1)

print(tbl_format_setup(new_tbl(trees, pillar_focus = "Volume"), width = 30))
})
})

Expand Down

0 comments on commit cf5556d

Please sign in to comment.