Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update image.R to enable control of image file size #1485

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions R/image.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ local_image <- function(
paste0("<img src=\"", uri, "\" style=\"height:", height, ";\">")
}


#' Helper function for adding a ggplot
#'
#' @description
Expand Down Expand Up @@ -294,6 +295,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.
#'
Expand Down Expand Up @@ -348,7 +354,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 `ggplot_image()`.")
Expand Down Expand Up @@ -376,9 +384,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))
Expand Down