Skip to content

Commit

Permalink
refactor: more consistent handling of graphics devices to supress Rpl…
Browse files Browse the repository at this point in the history
…ots.pdf
  • Loading branch information
jack-davison committed Dec 20, 2024
1 parent 846f2cc commit 666388f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Imports:
rlang,
sf,
stringr,
tidyr
tidyr,
withr
Suggests:
ggplot2,
ggspatial,
Expand Down
42 changes: 23 additions & 19 deletions R/utils-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -492,27 +492,31 @@ create_polar_markers <-
}

save_plot <- function(data, url) {
grDevices::pdf(NULL)

plot <- fun(data)

grDevices::png(
filename = url,
width = width * 300,
height = height * 300,
res = 300,
bg = "transparent",
type = "cairo",
antialias = "none"
# suppress Rplots.pdf
withr::with_cairo_pdf(
tempfile("PDFSINK"),
{
# create plot
plot <- fun(data)

# save plot
grDevices::png(
filename = url,
width = width * 300,
height = height * 300,
res = 300,
bg = "transparent",
type = "cairo",
antialias = "none"
)
print(plot)
grDevices::dev.off()

# return plot
return(plot)
}
)

print(plot)

while (grDevices::dev.cur() != 1L) {
grDevices::dev.off()
}

return(plot)
}

if (ncores == 1L) {
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-theme_static.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
test_that("theme can be added to a ggplot2 object", {
options(device = "cairo")

testthat::expect_no_error(
plt <- ggplot2::ggplot() + theme_static()
ggplot2::ggplot() + theme_static()
)
})

0 comments on commit 666388f

Please sign in to comment.