From 2058334d1672210edc83ecd7465a7ccf807e9a12 Mon Sep 17 00:00:00 2001 From: solvi808 <99327706+solvi808@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:43:14 +0100 Subject: [PATCH] Update image.R to enable control of image file size Added the parameters absolute_height and dpi to ggplot_image() in order to enable precise control of the saved image quality and absolute size, passed to ggsave() --- R/image.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/R/image.R b/R/image.R index 040de7e34e..48882d9491 100644 --- a/R/image.R +++ b/R/image.R @@ -250,6 +250,7 @@ local_image <- function( paste0("") } + #' Helper function for adding a ggplot #' #' @description @@ -300,6 +301,11 @@ local_image <- function( #' horizontally. The default value of `1.0` will neither compress nor expand #' the plot. #' +#' @param absolute_height this will be the absolute height of the image file to be saved on the disk, by default is 5. Changing this will affect the final filesize, so if there are many plots it is recommended to set this lower. +#' Note that changing this parameter works the same way as when using ggsave(), i.e. font size and such will be affected. +#' +#' @param dpi this modifies the resolution / quality of the saved image. In case we want to change only the resolution but keep the (absolute) image size. +#' #' @return A character object with an HTML fragment that can be placed inside of #' a cell. #' @@ -354,7 +360,9 @@ local_image <- function( ggplot_image <- function( plot_object, height = 100, - aspect_ratio = 1.0 + aspect_ratio = 1.0 , + absolute_height = 5 , + dpi = 100 ) { rlang::check_installed("ggplot2", "to use the `ggplot_image()` function.") @@ -382,9 +390,9 @@ ggplot_image <- function( filename = filename, plot = plot_object[[x]], device = "png", - dpi = 100, - width = 5 * aspect_ratio, - height = 5 + dpi = dpi, + width = absolute_height * aspect_ratio, + height = absolute_height ) on.exit(file.remove(filename))