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

What to do about plot dpi? #41

Open
Tracked by #44
kpagacz opened this issue May 12, 2022 · 1 comment
Open
Tracked by #44

What to do about plot dpi? #41

kpagacz opened this issue May 12, 2022 · 1 comment

Comments

@kpagacz
Copy link
Contributor

kpagacz commented May 12, 2022

Currently, teal.reporter saves any plot objects to files using the default dpi settings of the saving functions, disregarding the plot's dpi as rendered in shiny. This leads to discrepancies between what users see in the module and the previewer.

insightsengineering/teal.widgets#25 suggested the addition of teal.plot.dpi as a setting that governs the dpi in all teal packages. Right now, all of teal hardcodes the dpi at 72.

Things to consider:

  • hardcode the dpi of saving at 72 (where possible)
  • use teal.plot.dpi with the default of 72 to get the dpi

blocked by #165

@Polkas
Copy link
Contributor

Polkas commented Dec 12, 2022

The proper size for the images in different document types has a higher priority.
We inherit width and height from plot with settings for the report card purposes but not the res.
The issue is to take into account the dpi for the e.g. res arg in the png function.

We need additional set/get methods for dpi resolution (dpi) and return it from plot with settings.

set_content = function(content) {
checkmate::assert_multi_class(content, private$supported_plots)
path <- tempfile(fileext = ".png")
grDevices::png(filename = path, width = private$dim[1], height = private$dim[2])
tryCatch(
expr = {
if (inherits(content, "grob")) {
grid::grid.newpage()
grid::grid.draw(content)
} else if (inherits(content, c("gg", "Heatmap"))) { # "Heatmap" S4 from ComplexHeatmap
print(content)
} else if (inherits(content, "trellis")) {
grid::grid.newpage()
grid::grid.draw(grid::grid.grabExpr(print(content), warn = 0, wrap.grobs = TRUE))
}
super$set_content(path)
},
finally = grDevices::dev.off()
)
invisible(self)
},
#' @description Sets the title of this `PictureBlock`.
#'
#' @details throws if argument is not `character(1)`.
#'
#' @param title (`character(1)`) a string assigned to this `PictureBlock`
#' @return invisibly self
#' @examples
#' block <- teal.reporter:::PictureBlock$new()
#' block$set_title("Title")
#'
set_title = function(title) {
checkmate::assert_string(title)
private$title <- title
invisible(self)
},
#' @description Returns the title of this `PictureBlock`
#'
#' @return the content of this `PictureBlock`
#' @examples
#' block <- teal.reporter:::PictureBlock$new()
#' block$get_title()
#'
get_title = function() {
private$title
},
#' @description Sets the dimensions of this `PictureBlock`
#'
#' @param dim `numeric` figure dimensions (width and height) in pixels, length 2.
#' @return `self`
#' @examples
#' block <- teal.reporter:::PictureBlock$new()
#' block$set_dim(c(800, 600))
#'
set_dim = function(dim) {
checkmate::assert_numeric(dim, len = 2)
private$dim <- dim
invisible(self)
},
#' @description Returns the dimensions of this `PictureBlock`
#'
#' @return `numeric` the array of 2 numeric values representing width and height in pixels.
#' @examples
#' block <- teal.reporter:::PictureBlock$new()
#' block$get_dim()
#'
get_dim = function() {
private$dim
}

added to Report Card card

append_plot = function(plot, dim = NULL) {
pb <- PictureBlock$new()
if (!is.null(dim) && length(dim) == 2) {
pb$set_dim(dim)
}
pb$set_content(plot)
private$content <- append(private$content, pb)
invisible(self)
},

and finally added to each of tmc and tmg and teal.goshaw and teal.ospeys modules
https://github.com/insightsengineering/teal.modules.clinical/blob/fde3f1581c815a5be95e6d98b225f47706dc8307/R/tm_g_km.R#L786-L817

and added to plot with settings
https://github.com/insightsengineering/teal.widgets/blob/388121fa64c068bf7a5c4fc3f372123f3ef96cb8/R/plot_with_settings.R#L519

here we need to introduce the dpi for knitr #165

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants