Debian Codegraph @

R/imageutils.R

raw
1 startPNG <- function(filename, width, height, res, ...) {
2 pngfun <- if ((getOption('shiny.useragg') %||% TRUE) && is_installed("ragg")) {
3 ragg::agg_png
4 } else if (capabilities("aqua")) {
5 # i.e., png(type = 'quartz')
6 grDevices::png
7 } else if ((getOption('shiny.usecairo') %||% TRUE) && is_installed("Cairo")) {
8 Cairo::CairoPNG
9 } else {
10 # i.e., png(type = 'cairo')
11 grDevices::png
12 }
13
14 args <- list2(filename = filename, width = width, height = height, res = res, ...)
15
16 # It's possible for width/height to be NULL/numeric(0) (e.g., when using
17 # suspendWhenHidden=F w/ tabsetPanel(), see rstudio/shiny#1409), so when
18 # this happens let the device determine what the default size should be.
19 if (length(args$width) == 0) args$width <- NULL
20 if (length(args$height) == 0) args$height <- NULL
21
22 # Set a smarter default for the device's bg argument (based on thematic's global state).
23 # Note that, technically, this is really only needed for CairoPNG, since the other
24 # devices allow their bg arg to be overridden by par(bg=...), which thematic does prior
25 # to plot-time, but it shouldn't hurt to inform other the device directly as well
26 if (is.null(args$bg) && isNamespaceLoaded("thematic")) {
27 args$bg <- getThematicOption("bg", "white")
28 # auto vals aren't resolved until plot time, so if we see one, resolve it
29 if (isTRUE("auto" == args$bg)) {
30 args$bg <- getCurrentOutputInfo()[["bg"]]()
31 }
32 }
33
34 # Handle both bg and background device arg
35 # https://github.com/r-lib/ragg/issues/35
36 fmls <- names(formals(pngfun))
37 if (("background" %in% fmls) && (!"bg" %in% fmls)) {
38 if (is.null(args$background)) {
39 args$background <- args$bg
40 }
41 args$bg <- NULL
42 }
43
44 do.call(pngfun, args)
45 # Call plot.new() so that even if no plotting operations are performed at
46 # least we have a blank background. N.B. we need to set the margin to 0
47 # temporarily before plot.new() because when the plot size is small (e.g.
48 # 200x50), we will get an error "figure margin too large", which is triggered
49 # by plot.new() with the default (large) margin. However, this does not
50 # guarantee user's code in func() will not trigger the error -- they may have
51 # to set par(mar = smaller_value) before they draw base graphics.
52 op <- graphics::par(mar = rep(0, 4))
53 tryCatch(
54 graphics::plot.new(),
55 finally = graphics::par(op)
56 )
57
58 grDevices::dev.cur()
59 }
60
61 #' Capture a plot as a PNG file.
62 #'
63 #' The PNG graphics device used is determined in the following order:
64 #' * If the ragg package is installed (and the `shiny.useragg` is not
65 #' set to `FALSE`), then use [ragg::agg_png()].
66 #' * If a quartz device is available (i.e., `capabilities("aqua")` is
67 #' `TRUE`), then use `png(type = "quartz")`.
68 #' * If the Cairo package is installed (and the `shiny.usecairo` option
69 #' is not set to `FALSE`), then use [Cairo::CairoPNG()].
70 #' * Otherwise, use [grDevices::png()]. In this case, Linux and Windows
71 #' may not antialias some point shapes, resulting in poor quality output.
72 #'
73 #' @details
74 #' A `NULL` value provided to `width` or `height` is ignored (i.e., the
75 #' default `width` or `height` of the graphics device is used).
76 #'
77 #' @param func A function that generates a plot.
78 #' @param filename The name of the output file. Defaults to a temp file with
79 #' extension `.png`.
80 #' @param width Width in pixels.
81 #' @param height Height in pixels.
82 #' @param res Resolution in pixels per inch. This value is passed to the
83 #' graphics device. Note that this affects the resolution of PNG rendering in
84 #' R; it won't change the actual ppi of the browser.
85 #' @param ... Arguments to be passed through to the graphics device. These can
86 #' be used to set the width, height, background color, etc.
87 #'
88 #' @return A path to the newly generated PNG file.
89 #'
90 #' @export
91 plotPNG <- function(func, filename=tempfile(fileext='.png'),
92 width=400, height=400, res=72, ...) {
93 dv <- startPNG(filename, width, height, res, ...)
94 on.exit(grDevices::dev.off(dv), add = TRUE)
95 func()
96
97 filename
98 }
99
100 createGraphicsDevicePromiseDomain <- function(which = dev.cur()) {
101 force(which)
102
103 new_promise_domain(
104 wrapOnFulfilled = function(onFulfilled) {
105 force(onFulfilled)
106 function(...) {
107 old <- dev.cur()
108 dev.set(which)
109 on.exit(dev.set(old))
110
111 onFulfilled(...)
112 }
113 },
114 wrapOnRejected = function(onRejected) {
115 force(onRejected)
116 function(...) {
117 old <- dev.cur()
118 dev.set(which)
119 on.exit(dev.set(old))
120
121 onRejected(...)
122 }
123 },
124 wrapSync = function(expr) {
125 old <- dev.cur()
126 dev.set(which)
127 on.exit(dev.set(old))
128
129 force(expr)
130 }
131 )
132 }