Skip to content

Commit e156756

Browse files
committed
Refactor header formatting in tbl_format_header to enhance covariate label alignment and improve overall output presentation.
1 parent b698bad commit e156756

File tree

4 files changed

+39
-63
lines changed

4 files changed

+39
-63
lines changed

R/pillar_utlis.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pillar___strwrap2 <- function (x, width, indent, strip.spaces = TRUE)
1919

2020
pillar___wrap <- function (..., indent=0, prefix="", width, strip.spaces = TRUE)
2121
{
22+
2223
x <- paste0(..., collapse="")
2324
wrapped <- pillar___strwrap2(x, width - get_extent(prefix), indent, strip.spaces = strip.spaces)
2425
wrapped <- paste0(prefix, wrapped)

R/print_methods.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ but they do not completely overlap.")
269269
add_attr(names(col_), "covariate_names") |>
270270

271271
add_attr(
272-
colnames(out_sub),
272+
# Get the actual column names that will be printed on screen
273+
# This uses tibble's internal method to determine visible columns
274+
pillar::tbl_format_setup(out_sub, width = getOption("width", 80) + 4)$body[1] |> as.character(),
273275
"printed_colnames"
274276
) %>%
275277
add_attr(
@@ -284,6 +286,7 @@ but they do not completely overlap.")
284286
invisible(x)
285287
}
286288

289+
287290
print_tidyprint_1(x, ...)
288291
invisible(x)
289292

R/tidyprint_1_utlis.R

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,35 +155,44 @@ tbl_format_header.SE_print_abstraction <- function(x, setup, ...) {
155155
number_of_total_rows = (x |> attr("number_of_features")) * (x |> attr("number_of_samples"))
156156

157157
printed_colnames <- x |> attr("printed_colnames")
158-
159-
# Identify covariate columns: those from colData
160-
# Assume covariate columns are after .sample, .feature, .count, and assay columns
161-
# We'll use heuristics: find the first and last covariate column positions
162158

163-
# .feature and .samples SHOULD BE A GLOBAL VARIABLE CREATED ONES
164-
# SO IT CAN BE CHANGED ACROSS THE PACKAGE
165-
# THIS BREAKS IF I HAVE ROWDATA
166-
covariate_candidates <- setdiff(printed_colnames, c(".sample", ".feature", "|", assay_names))
167-
# Remove gene/rowData columns if possible (e.g., chromosome, gene_feature, ...)
168-
# For now, just use all columns after .count and before gene_feature as covariates
169-
first_covariate <- which(printed_colnames %in% covariate_candidates)[1]
170-
last_covariate <- which(printed_colnames %in% covariate_candidates) |> tail(1)
171-
last_covariate <- if (length(last_covariate) > 0) max(last_covariate) else NA
172-
173-
# Only add header if there are covariate columns
174-
covariate_header <- NULL
159+
# Find the positions of all '|' characters in the string
160+
pipe_positions <- stringr::str_locate_all(printed_colnames, "\\|")[[1]][, "start"]
175161

176-
if (!is.na(first_covariate) && !is.na(last_covariate) && last_covariate >= first_covariate) {
177-
covariate_header <- format_covariate_header(
178-
separator_row = separator_row,
179-
printed_colnames = printed_colnames,
180-
covariate_names = covariate_names,
181-
number_of_total_rows = number_of_total_rows,
182-
label = " COVARIATES "
183-
)
184-
covariate_header <- cli::col_br_blue(covariate_header)
162+
# Calculate character length to the start of the second '|'
163+
chars_to_second_pipe <- pipe_positions[2] - 2
164+
165+
# Check if there's a third pipe
166+
if (length(pipe_positions) >= 3) {
167+
# Calculate character length between second and third pipe
168+
chars_to_third_pipe <- pipe_positions[3] - pipe_positions[2] - 2
169+
} else {
170+
# Calculate character length to the end of the line
171+
chars_to_third_pipe <- nchar(printed_colnames) - pipe_positions[2]
185172
}
186173

174+
label = " COVARIATES "
175+
label_length <- nchar(label)
176+
177+
# Center the label in the total covariate width, using only dashes and the label
178+
left_pad <- floor((chars_to_third_pipe - label_length) / 2)
179+
right_pad <- chars_to_third_pipe - label_length - left_pad
180+
merged_label <- paste0(
181+
paste(rep("-", left_pad), collapse = ""),
182+
label,
183+
paste(rep("-", right_pad), collapse = "")
184+
)
185+
186+
# Add '|' at the beginning and end
187+
merged_label <- paste0("|", merged_label, "|")
188+
189+
# Pad with the spaces until chars to second pipe
190+
merged_label <- c(paste(rep(" ", chars_to_second_pipe), collapse = ""), merged_label) |>
191+
paste0(collapse = "")
192+
193+
covariate_header <- cli::col_br_blue(merged_label)
194+
195+
187196
# Compose the main header as before
188197
if (all(names2(named_header) == "")) {
189198
header <- named_header

R/tidyse_utils.R

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,6 @@
44
#' @importFrom dplyr full_join
55
# This file is a replacement of the unexported functions in the tibble
66
# package, in order to specify "tibble abstraction in the header"
7-
8-
#' @importFrom rlang names2
9-
#' @importFrom pillar align
10-
#' @importFrom pillar get_extent
11-
#' @importFrom pillar style_subtle
12-
#' @importFrom pillar tbl_format_header
13-
#' @importFrom cli col_br_black
14-
#' @importFrom tibble as_tibble
15-
#' @export
16-
tbl_format_header.tidySummarizedExperiment <- function(x, setup, ...) {
17-
18-
number_of_features <- x |> attr("number_of_features")
19-
number_of_samples <- x |> attr("number_of_samples")
20-
named_header <- x |> attr("named_header")
21-
assay_names <- x |> attr("assay_names")
22-
23-
24-
if (all(names2(named_header) == "")) {
25-
header <- named_header
26-
} else {
27-
header <-
28-
paste0(
29-
align(paste0(names2(named_header), ":"), space=NBSP),
30-
" ",
31-
named_header
32-
) %>%
33-
# Add further info single-cell
34-
append( cli::col_br_black( sprintf(
35-
" Features=%s | Samples=%s | Assays=%s",
36-
number_of_features,
37-
number_of_samples,
38-
assay_names %>% paste(collapse=", ")
39-
)), after = 1)
40-
}
41-
style_subtle(pillar___format_comment(header, width=setup$width))
42-
}
43-
447
check_if_assays_are_NOT_overlapped <- function(se, dim = "cols") {
458

469
stopifnot(dim %in% c("rows", "cols"))

0 commit comments

Comments
 (0)