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

add teal.plot_dpi option #26

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# teal.widgets 0.1.0.9001

### Enhancements

* The `option` `teal.plot_dpi` controls the `dpi` for rendering and downloading plots in `plot_with_settings`. If `option` not used then a default of `72dpi` is used
(which is unchanged from the previous behavior).

### Bug fixes

* Fixed an edge case bug where zooming in or out from the browser would cause a plot of a `grob` object to not re-render instead displaying a blank white space.
Expand Down
50 changes: 34 additions & 16 deletions R/plot_with_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ plot_with_settings_ui <- function(id) {
#' one of `"left"` (default), `"center"`, `"right"` or `"justify"`. The alignment of the graph on
#' the main page.
#'
#' @details By default the plot is rendered with `72 dpi`. In order to change this, to for example 96 set
#' `options(teal.plot_dpi = 96)`. The minimum allowed `dpi` value is `24` and it must be a whole number.
#' If an invalid value is set then the default value is used and a warning is outputted to the console.
#'
#' @examples
#' \dontrun{
#' library(shiny)
Expand Down Expand Up @@ -305,19 +309,22 @@ plot_with_settings_srv <- function(id,
}
})

output$plot_modal <- output$plot_main <- renderPlot({
if (plot_type() == "gg" && dblclicking) {
plot_r() +
ggplot2::coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)
} else if (plot_type() == "grob") {
# calling grid.draw on plot_r() is needed;
# otherwise the plot will not re-render if the user triggers the zoom in or out feature of the browser.
grid::grid.newpage()
grid::grid.draw(plot_r())
} else {
plot_r()
}
})
output$plot_modal <- output$plot_main <- renderPlot(
expr = {
if (plot_type() == "gg" && dblclicking) {
plot_r() +
ggplot2::coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)
} else if (plot_type() == "grob") {
# calling grid.draw on plot_r() is needed;
# otherwise the plot will not re-render if the user triggers the zoom in or out feature of the browser.
grid::grid.newpage()
grid::grid.draw(plot_r())
} else {
plot_r()
}
},
res = get_plot_dpi()
)

output$plot_out_main <- renderUI({
div(
Expand Down Expand Up @@ -487,11 +494,11 @@ type_download_srv <- function(id, plot_reactive, plot_type, plot_w, default_w, p
width <- `if`(!is.null(plot_w()), plot_w(), default_w())
height <- `if`(!is.null(plot_h()), plot_h(), default_h())

# svg and pdf have width in inches and 1 inch = 72 pixels
# svg and pdf have width in inches and 1 inch = get_plot_dpi() pixels
switch(input$file_format,
png = grDevices::png(file, width, height),
pdf = grDevices::pdf(file, width / 72, height / 72),
svg = grDevices::svg(file, width / 72, height / 72)
pdf = grDevices::pdf(file, width / get_plot_dpi(), height / get_plot_dpi()),
svg = grDevices::svg(file, width / get_plot_dpi(), height / get_plot_dpi())
)

if (plot_type() == "other") {
Expand Down Expand Up @@ -574,3 +581,14 @@ clean_brushedPoints <- function(data, brush) { # nolintr
df <- df[rowSums(is.na(df)) != ncol(df), ]
df
}


get_plot_dpi <- function() {
default_dpi <- 72
dpi <- getOption("teal.plot_dpi", default_dpi)
if (!checkmate::test_integerish(dpi, lower = 24, any.missing = FALSE, len = 1)) {
warning(paste("Invalid value for option 'teal.plot_dpi', therefore defaulting to", default_dpi, "dpi"))
dpi <- default_dpi
}
dpi
}
5 changes: 5 additions & 0 deletions man/plot_with_settings_srv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions tests/testthat/test-plot_with_settings.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
testthat::test_that("if teal.plot_dpi is not set then get_plot_dpi returns 72 ", {
withr::with_options(
list(teal.plot_dpi = NULL),
testthat::expect_equal(get_plot_dpi(), 72)
)
})

testthat::test_that("if teal.plot_dpi is an integer value at least 24 then get_plot_dpi returns its value", {
withr::with_options(
list(teal.plot_dpi = 24),
testthat::expect_equal(get_plot_dpi(), 24)
)
withr::with_options(
list(teal.plot_dpi = 48),
testthat::expect_equal(get_plot_dpi(), 48)
)
withr::with_options(
list(teal.plot_dpi = 72),
testthat::expect_equal(get_plot_dpi(), 72)
)
withr::with_options(
list(teal.plot_dpi = 96),
testthat::expect_equal(get_plot_dpi(), 96)
)
})

testthat::test_that("if teal.plot_dpi is an integer value less 24 then get_plot_dpi returns 72", {
testthat::expect_warning(
withr::with_options(
list(teal.plot_dpi = 23),
testthat::expect_equal(get_plot_dpi(), 72)
),
"teal.plot_dpi"
)
testthat::expect_warning(
withr::with_options(
list(teal.plot_dpi = 0),
testthat::expect_equal(get_plot_dpi(), 72)
),
"teal.plot_dpi"
)
})

testthat::test_that("if teal.plot_dpi is not an integer value then get_plot_dpi returns 72", {
testthat::expect_warning(
withr::with_options(
list(teal.plot_dpi = c(72, 96)),
testthat::expect_equal(get_plot_dpi(), 72)
),
"teal.plot_dpi"
)

testthat::expect_warning(
withr::with_options(
list(teal.plot_dpi = "foo"),
testthat::expect_equal(get_plot_dpi(), 72)
),
"teal.plot_dpi"
)
})