diff --git a/R/tbl-format-header.R b/R/tbl-format-header.R index 745c2d593..8f7156ef3 100644 --- a/R/tbl-format-header.R +++ b/R/tbl-format-header.R @@ -33,7 +33,7 @@ tbl_format_header <- function(x, setup, ...) { #' @export tbl_format_header.tbl <- function(x, setup, ...) { named_header <- setup$tbl_sum - focus <- attr(x, "pillar_focus") + focus <- setup$focus if (!is.null(focus)) { named_header <- c(named_header, "Focus columns" = collapse(tick_if_needed(focus))) } diff --git a/R/tbl-format-setup.R b/R/tbl-format-setup.R index 6038e19b2..70be225b5 100644 --- a/R/tbl-format-setup.R +++ b/R/tbl-format-setup.R @@ -130,6 +130,8 @@ tbl_format_setup.tbl <- function(x, width, ..., rows_missing <- 0L } + focus <- update_focus(focus, x) + # Header tbl_sum <- tbl_sum(x) @@ -169,10 +171,22 @@ tbl_format_setup.tbl <- function(x, width, ..., extra_cols = extra_cols, extra_cols_total = extra_cols_total, max_footer_lines = max_footer_lines, - abbrev_cols = abbrev_cols + abbrev_cols = abbrev_cols, + focus = focus ) } +update_focus <- function(focus, x) { + if (is_null(focus)) { + focus <- attr(x, "pillar_focus") + } + focus <- intersect(focus, names(x)) + if (is_empty(focus)) { + return(NULL) + } + focus +} + #' Construct a setup object for formatting #' #' @description @@ -207,7 +221,8 @@ 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) { + abbrev_cols, + focus) { trunc_info <- list( x = x, df = df, @@ -219,7 +234,8 @@ new_tbl_format_setup <- function(x, df, width, tbl_sum, body, extra_cols = extra_cols, extra_cols_total = extra_cols_total, max_footer_lines = max_footer_lines, - abbrev_cols = abbrev_cols + abbrev_cols = abbrev_cols, + focus = focus ) structure(trunc_info, class = "pillar_tbl_format_setup")