From aa9c38600708a8b2f888dee56e8bfb3c9f15a8e8 Mon Sep 17 00:00:00 2001 From: qclayssen Date: Sun, 19 Nov 2023 16:21:49 +0100 Subject: [PATCH 1/3] Enhance print method for SingleCellExperiment Implemented red coloring for special and needed columns in SingleCellExperiment objects. This update aligns with the requirements outlined in issue #9, enhancing the visibility and distinction of view-only columns in the printed output. --- NAMESPACE | 3 +++ R/print_method.R | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 7020d9c..d99f510 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -61,7 +61,10 @@ importFrom(SummarizedExperiment,assay) importFrom(SummarizedExperiment,assayNames) importFrom(SummarizedExperiment,assays) importFrom(SummarizedExperiment,colData) +importFrom(crayon,red) +importFrom(dplyr,across) importFrom(dplyr,add_count) +importFrom(dplyr,all_of) importFrom(dplyr,any_of) importFrom(dplyr,arrange) importFrom(dplyr,contains) diff --git a/R/print_method.R b/R/print_method.R index bdef4ae..4bf6d1f 100755 --- a/R/print_method.R +++ b/R/print_method.R @@ -46,20 +46,35 @@ tbl_format_header.tidySingleCellExperiment <- function(x, setup, ...) { #' @return Prints a message to the console describing #' the contents of the `tidySingleCellExperiment`. #' +#' @param x A SingleCellExperiment object. +#' @param n (optional) The maximum number of rows to show. +#' @param width (optional) The maximum width of the output. #' @examples #' data(pbmc_small) #' print(pbmc_small) -#' +#' #' @importFrom vctrs new_data_frame #' @importFrom SummarizedExperiment assayNames +#' @importFrom crayon red +#' @importFrom dplyr mutate across all_of #' @export print.SingleCellExperiment <- function(x, ..., n=NULL, width=NULL) { + + special_cols <- get_special_datasets(x) %>% + map(~ .x %>% colnames()) %>% + unlist() %>% + unique() + + # Convert to tibble and apply coloring row-wise x |> as_tibble(n_dimensions_to_return=5) |> new_data_frame(class=c("tidySingleCellExperiment", "tbl")) %>% add_attr(nrow(x), "number_of_features") %>% - add_attr(assayNames(x), "assay_names") %>% - print() - + add_attr(assayNames(x), "assay_names") |> + apply(1, function(row) { + styled_row <- ifelse(names(row) %in% special_cols, crayon::red(row), row) + cat(paste(styled_row, collapse = " "), "\n") + }) + invisible(x) } From 46eb2dc8695fef2637cd627c3f7485dc9e881611 Mon Sep 17 00:00:00 2001 From: qclayssen Date: Sun, 19 Nov 2023 17:27:08 +0100 Subject: [PATCH 2/3] fix print coloring that didn't output tiddle --- R/print_method.R | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/R/print_method.R b/R/print_method.R index 4bf6d1f..178e999 100755 --- a/R/print_method.R +++ b/R/print_method.R @@ -67,14 +67,12 @@ print.SingleCellExperiment <- function(x, ..., n=NULL, width=NULL) { # Convert to tibble and apply coloring row-wise x |> + mutate(across(all_of(special_cols), ~ crayon::red(as.character(.))))|> as_tibble(n_dimensions_to_return=5) |> new_data_frame(class=c("tidySingleCellExperiment", "tbl")) %>% add_attr(nrow(x), "number_of_features") %>% - add_attr(assayNames(x), "assay_names") |> - apply(1, function(row) { - styled_row <- ifelse(names(row) %in% special_cols, crayon::red(row), row) - cat(paste(styled_row, collapse = " "), "\n") - }) + add_attr(assayNames(x), "assay_names")|> + print(tbl, n = n, width = width) invisible(x) } From 3b740c83f3220e1baa3f0d5dca50bbb2a62086ec Mon Sep 17 00:00:00 2001 From: qclayssen Date: Thu, 11 Jan 2024 23:46:06 +0000 Subject: [PATCH 3/3] add crayon for color print --- R/print_method.R | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/R/print_method.R b/R/print_method.R index 178e999..666bb0d 100755 --- a/R/print_method.R +++ b/R/print_method.R @@ -57,22 +57,28 @@ tbl_format_header.tidySingleCellExperiment <- function(x, setup, ...) { #' @importFrom SummarizedExperiment assayNames #' @importFrom crayon red #' @importFrom dplyr mutate across all_of +#' @importFrom crayon red #' @export print.SingleCellExperiment <- function(x, ..., n=NULL, width=NULL) { + # Retrieve special view-only columns + special_cols <- get_special_columns(x) + + # Convert to tibble + tibble_x <- x |> as_tibble(n_dimensions_to_return=5) - special_cols <- get_special_datasets(x) %>% - map(~ .x %>% colnames()) %>% - unlist() %>% - unique() + # Modify column titles for special columns + col_titles <- names(tibble_x) + modified_col_titles <- ifelse(col_titles %in% special_cols, + crayon::red(as.character(col_titles)), + col_titles) - # Convert to tibble and apply coloring row-wise - x |> - mutate(across(all_of(special_cols), ~ crayon::red(as.character(.))))|> - as_tibble(n_dimensions_to_return=5) |> - new_data_frame(class=c("tidySingleCellExperiment", "tbl")) %>% + names(tibble_x) <- modified_col_titles + + # Print the tibble with modified column titles + new_data_frame(tibble_x, class=c("tidySingleCellExperiment", "tbl")) %>% add_attr(nrow(x), "number_of_features") %>% - add_attr(assayNames(x), "assay_names")|> - print(tbl, n = n, width = width) + add_attr(assayNames(x), "assay_names") %>% + print(n = n, width = width) invisible(x) -} +} \ No newline at end of file