From 899a2eebc32af2d9d57d1183032b51f42759360d Mon Sep 17 00:00:00 2001 From: Chris Sobczak Date: Mon, 6 Jul 2026 15:52:27 -0700 Subject: Add sorting for heatmap --- R/heatmap.R | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'R/heatmap.R') diff --git a/R/heatmap.R b/R/heatmap.R index cb6c8f8..3351045 100644 --- a/R/heatmap.R +++ b/R/heatmap.R @@ -21,23 +21,34 @@ heatmap <- function(R, title = NULL, labels = FALSE, order = NULL){ R_list <- tryCatch({ if(is.matrix(R)){ Rm <- R - cnames <- colnames(Rm) - rnames <- rownames(Rm) }else{ Rm <- as.matrix(R) - cnames <- colnames(Rm) - rnames <- rownames(Rm) } + + cnames <- colnames(Rm) + rnames <- rownames(Rm) + if(is.null(cnames)){ + cnames <- as.character(seq_len(ncol(Rm))) + } + if(is.null(rnames)){ + rnames <- as.character(seq_len(nrow(Rm))) + } + if(nrow(Rm) == ncol(Rm)){ if(!is.null(order)){ - order <- get_order(Rm) - M <- Rm[order, order] + if(length(order) != nrow(Rm)){ + stop('Provided order is not the same length as the width of R') + }else{ + rownames(Rm) <- rnames + colnames(Rm) <- cnames + Rm <- Rm[order, order] + } } list( - M = M[upper.tri(M)], + Rm = Rm[upper.tri(Rm)], cnames = cnames, rnames = rnames, - p = ncol(M) + p = ncol(Rm) ) }else{ stop('Input must be coercible to a square matrix') @@ -47,13 +58,6 @@ heatmap <- function(R, title = NULL, labels = FALSE, order = NULL){ stop(paste('Invalid input for heatmap:', e$message)) }) - if(is.null(R_list$cnames)){ - R_list$cnames <- as.character(seq_len(R_list$p)) - } - if(is.null(R_list$rnames)){ - R_list$rnames <- as.character(seq_len(R_list$p)) - } - # Now get a df from the upper triangle m <- length(R_list$R) n <- as.integer((1 + sqrt(1 + 8 * m)) / 2) -- cgit v1.2.3