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 full support for ragg #4388

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Imports:
withr (>= 2.0.0)
Suggests:
covr,
ragg,
dplyr,
ggplot2movies,
hexbin,
Expand Down
34 changes: 25 additions & 9 deletions R/save.r
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,34 @@ plot_dev <- function(device, filename = NULL, dpi = 300) {
force(dpi)

if (is.function(device)) {
if ("file" %in% names(formals(device))) {
dev <- function(filename, ...) device(file = filename, ...)
return(dev)
} else {
return(device)
args <- formals(device)
call_args <- list()
if ("file" %in% names(args)) {
call_args$file <- filename
}
if ("res" %in% names(args)) {
call_args$res <- dpi
}
if ("units" %in% names(args)) {
call_args$units <- 'in'
clauswilke marked this conversation as resolved.
Show resolved Hide resolved
}
dev <- function(...) do.call(device, modify_list(list(...), call_args))
return(dev)
}

eps <- function(filename, ...) {
grDevices::postscript(file = filename, ..., onefile = FALSE, horizontal = FALSE,
paper = "special")
}
if (requireNamespace('ragg', quietly = TRUE)) {
png_dev <- ragg::agg_png
jpeg_dev <- ragg::agg_jpeg
tiff_dev <- ragg::agg_tiff
} else {
png_dev <- grDevices::png
jpeg_dev <- grDevices::jpeg
tiff_dev <- grDevices::tiff
}
devices <- list(
eps = eps,
ps = eps,
Expand All @@ -178,11 +194,11 @@ plot_dev <- function(device, filename = NULL, dpi = 300) {
svg = function(filename, ...) svglite::svglite(file = filename, ...),
emf = function(...) grDevices::win.metafile(...),
wmf = function(...) grDevices::win.metafile(...),
png = function(...) grDevices::png(..., res = dpi, units = "in"),
jpg = function(...) grDevices::jpeg(..., res = dpi, units = "in"),
jpeg = function(...) grDevices::jpeg(..., res = dpi, units = "in"),
png = function(...) png_dev(..., res = dpi, units = "in"),
jpg = function(...) jpeg_dev(..., res = dpi, units = "in"),
jpeg = function(...) jpeg_dev(..., res = dpi, units = "in"),
bmp = function(...) grDevices::bmp(..., res = dpi, units = "in"),
tiff = function(...) grDevices::tiff(..., res = dpi, units = "in")
tiff = function(...) tiff_dev(..., res = dpi, units = "in")
)

if (is.null(device)) {
Expand Down
8 changes: 2 additions & 6 deletions tests/testthat/test-ggsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,19 @@ test_that("scale multiplies height & width", {

# plot_dev ---------------------------------------------------------------------

test_that("function is passed back unchanged", {
expect_equal(plot_dev(png), png)
})

test_that("unknown device triggers error", {
expect_error(plot_dev("xyz"), "Unknown graphics device")
expect_error(plot_dev(NULL, "test.xyz"), "Unknown graphics device")
})


test_that("text converted to function", {
expect_identical(body(plot_dev("png"))[[1]], quote(grDevices::png))
expect_identical(body(plot_dev("png"))[[1]], quote(png_dev))
expect_identical(body(plot_dev("pdf"))[[1]], quote(grDevices::pdf))
})

test_that("if device is NULL, guess from extension", {
expect_identical(body(plot_dev(NULL, "test.png"))[[1]], quote(grDevices::png))
expect_identical(body(plot_dev(NULL, "test.png"))[[1]], quote(png_dev))
})

# parse_dpi ---------------------------------------------------------------
Expand Down