diff --git a/R/pheatmap.r b/R/pheatmap.r index 8c2e919..b5a4ee4 100644 --- a/R/pheatmap.r +++ b/R/pheatmap.r @@ -272,8 +272,13 @@ convert_annotations = function(annotation, annotation_colors){ new[, i] = b[a] } else{ - a = cut(a, breaks = 100) - new[, i] = colorRampPalette(b)(100)[a] + if(all(is.na(a))){ + new[, i] = rep(NA, length(a)) + } + else { + a = cut(a, breaks = 100) + new[, i] = colorRampPalette(b)(100)[a] + } } } return(as.matrix(new)) @@ -335,6 +340,11 @@ draw_annotation_legend = function(annotation, annotation_colors, border_color, . res = gList() for(i in names(annotation)){ + + if(all(is.na(annotation[[i]]))){ # skip label for elements with only missing values + next() + } + res[[i]] = textGrob(i, x = 0, y = y, vjust = 1, hjust = 0, gp = gpar(fontface = "bold", ...)) y = y - 1.5 * text_height @@ -1089,10 +1099,13 @@ pheatmap = function(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name = " } # Draw heatmap - pdf(file = NULL) - gt = heatmap_motor(mat, border_color = border_color, cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, tree_col = tree_col, tree_row = tree_row, filename = filename, width = width, height = height, breaks = breaks, color = color, legend = legend, annotation_row = annotation_row, annotation_col = annotation_col, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, gaps_row = gaps_row, gaps_col = gaps_col, labels_row = labels_row, labels_col = labels_col, ...) - dev.off() - + tryCatch({ + pdf(file = NULL) + gt = heatmap_motor(mat, border_color = border_color, cellwidth = cellwidth, cellheight = cellheight, treeheight_col = treeheight_col, treeheight_row = treeheight_row, tree_col = tree_col, tree_row = tree_row, filename = filename, width = width, height = height, breaks = breaks, color = color, legend = legend, annotation_row = annotation_row, annotation_col = annotation_col, annotation_colors = annotation_colors, annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, annotation_names_col = annotation_names_col, main = main, fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, gaps_row = gaps_row, gaps_col = gaps_col, labels_row = labels_row, labels_col = labels_col, ...) + }, finally={ + dev.off() + }) + if(is.na(filename) & !silent){ grid.newpage() grid.draw(gt)