From f20c0f87202bcd20e4fcb56543cf30e45fe620c0 Mon Sep 17 00:00:00 2001 From: vertesy Date: Wed, 24 Jul 2024 16:41:30 +0200 Subject: [PATCH] bug in wplot_save_pheatmap The problem was in width and height calculation --- R/MarkdownReports.R | 9 ++++++--- man/wplot_save_pheatmap.Rd | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/R/MarkdownReports.R b/R/MarkdownReports.R index c0a7f32..a0977f2 100644 --- a/R/MarkdownReports.R +++ b/R/MarkdownReports.R @@ -407,6 +407,7 @@ wplot_save_this <- function(plotname = ww.autoPlotName(), #' @description Save pheatmap object. Modified from: #' https://stackoverflow.com/questions/43051525/how-to-draw-pheatmap-plot-to-screen-and-also-save-to-file #' @param x The pheatmap object to save. +#' @param data data matrix used to create the pheatmap object. Only used to define plot height and width. #' @param suffix Suffix to File name. Default: 'heatmap'. #' @param plotname Name of the plot. Default: substitute(x). File saved as .pdf, inside working #' directory. @@ -429,11 +430,12 @@ wplot_save_this <- function(plotname = ww.autoPlotName(), #' ph.test <- pheatmap::pheatmap(test) #' wplot_save_pheatmap(ph.test) wplot_save_pheatmap <- function(x, + data = NULL, suffix = "heatmap", plotname = as.character(substitute(x)), add = 1, - width = max(length(x$tree_col$labels)+add, 4), - height = max(length(x$tree_row$labels)-add, 4), + width = max(if(is.null(data)) 7 else ncol(data) + add, 4), + height = max(if(is.null(data)) 7 else nrow(data) - add, 4), pdf = TRUE, png = FALSE, png_res = 100, # NA @@ -441,7 +443,8 @@ wplot_save_pheatmap <- function(x, mdlink = TRUE) { # stopifnot(inherits(x, "pheatmap"), - is.character(suffix), is.character(plotname), is.numeric(add), is.numeric(width), is.numeric(height), + is.character(suffix), is.character(plotname), nchar(plotname) < 200, + is.numeric(add), is.numeric(width), is.numeric(height), is.logical(pdf), is.logical(png), is.numeric(png_res), is.numeric(png_dim_factor), is.logical(mdlink) ) diff --git a/man/wplot_save_pheatmap.Rd b/man/wplot_save_pheatmap.Rd index 492e3ca..972281e 100644 --- a/man/wplot_save_pheatmap.Rd +++ b/man/wplot_save_pheatmap.Rd @@ -6,10 +6,12 @@ \usage{ wplot_save_pheatmap( x, + data = NULL, suffix = "heatmap", - plotname = substitute(x), - width = nrow(x) + 4, - height = ncols(x) + 4, + plotname = as.character(substitute(x)), + add = 1, + width = max(if (is.null(data)) 7 else ncol(data) + add, 4), + height = max(if (is.null(data)) 7 else nrow(data) - add, 4), pdf = TRUE, png = FALSE, png_res = 100, @@ -20,8 +22,15 @@ wplot_save_pheatmap( \arguments{ \item{x}{The pheatmap object to save.} +\item{data}{data matrix used to create the pheatmap object. Only used to define plot height and width.} + \item{suffix}{Suffix to File name. Default: 'heatmap'.} +\item{plotname}{Name of the plot. Default: substitute(x). File saved as .pdf, inside working +directory.} + +\item{add}{Constant to add to width and height. Default: 2.} + \item{width}{width of the plot in inches.} \item{height}{height of the plot in inches.} @@ -36,8 +45,6 @@ wplot_save_pheatmap( \item{mdlink}{Insert a .pdf and a .png image link in the markdown report, set by "path_of_report".} - -\item{filename}{File name (saved as .pdf, inside working directory).} } \description{ Save pheatmap object. Modified from: