Debian Codegraph @

R/devices.R

raw
1 #' Graphics devices
2 #'
3 #' Temporarily use a graphics device.
4 #'
5 #' @name devices
6 #' @aliases with_dev with_device
7 #' @template with
8 #' @param new \code{[named character]}\cr New graphics device
9 #' @param ... Additional arguments passed to the graphics device.
10 #' @param .local_envir `[environment]`\cr The environment to use for scoping.
11 #' @details
12 #' * `with_bmp()` and `local_bmp()` wrap around [grDevices::bmp()].
13 #' * `with_cairo_pdf()` and `local_cairo_pdf()` wrap around [grDevices::cairo_pdf()].
14 #' * `with_cairo_ps()` and `local_cairo_ps()` wrap around [grDevices::cairo_ps()].
15 #' * `with_pdf()` and `local_pdf()` wrap around [grDevices::pdf()].
16 #' * `with_postscript()` and `local_postscript()` wrap around [grDevices::postscript()].
17 #' * `with_svg()` and `local_svg()` wrap around [grDevices::svg()].
18 #' * `with_tiff()` and `local_tiff()` wrap around [grDevices::tiff()].
19 #' * `with_xfig()` and `local_xfig()` wrap around [grDevices::xfig()].
20 #' * `with_png()` and `local_png()` wrap around [grDevices::png()].
21 #' * `with_jpeg()` and `local_jpeg()` wrap around [grDevices::jpeg()].
22 #'
23 #' @seealso \code{\link[grDevices]{Devices}}
24 #' @examples
25 #' # dimensions are in inches
26 #' with_pdf(file.path(tempdir(), "test.pdf"), width = 7, height = 5,
27 #' plot(runif(5))
28 #' )
29 #'
30 #' # dimensions are in pixels
31 #' with_png(file.path(tempdir(), "test.png"), width = 800, height = 600,
32 #' plot(runif(5))
33 #' )
34 NULL
35
36 #' @describeIn devices BMP device
37 #' @export
38 with_bmp <- function(new, code, ...) {
39 local_bmp(new, ...)
40 code
41 }
42
43 #' @rdname devices
44 #' @export
45 local_bmp <- function(new, ..., .local_envir = parent.frame()) {
46 grDevices::bmp(new, ...)
47 defer_dev_close(.local_envir)
48 }
49
50 #' @describeIn devices CAIRO_PDF device
51 #' @export
52 with_cairo_pdf <- function(new, code, ...) {
53 local_cairo_pdf(new, ...)
54 code
55 }
56
57 #' @rdname devices
58 #' @export
59 local_cairo_pdf <- function(new, ..., .local_envir = parent.frame()) {
60 grDevices::cairo_pdf(new, ...)
61 defer_dev_close(.local_envir)
62 }
63
64 #' @describeIn devices CAIRO_PS device
65 #' @export
66 with_cairo_ps <- function(new, code, ...) {
67 local_cairo_ps(new, ...)
68 code
69 }
70
71 #' @rdname devices
72 #' @export
73 local_cairo_ps <- function(new, ..., .local_envir = parent.frame()) {
74 grDevices::cairo_ps(new, ...)
75 defer_dev_close(.local_envir)
76 }
77
78 #' @describeIn devices PDF device
79 #' @export
80 with_pdf <- function(new, code, ...) {
81 local_pdf(new, ...)
82 code
83 }
84
85 #' @rdname devices
86 #' @export
87 local_pdf <- function(new, ..., .local_envir = parent.frame()) {
88 grDevices::pdf(new, ...)
89 defer_dev_close(.local_envir)
90 }
91
92 #' @describeIn devices POSTSCRIPT device
93 #' @export
94 with_postscript <- function(new, code, ...) {
95 local_postscript(new, ...)
96 code
97 }
98
99 #' @rdname devices
100 #' @export
101 local_postscript <- function(new, ..., .local_envir = parent.frame()) {
102 grDevices::postscript(new, ...)
103 defer_dev_close(.local_envir)
104 }
105
106 #' @describeIn devices SVG device
107 #' @export
108 with_svg <- function(new, code, ...) {
109 local_svg(new, ...)
110 code
111 }
112
113 #' @rdname devices
114 #' @export
115 local_svg <- function(new, ..., .local_envir = parent.frame()) {
116 grDevices::svg(new, ...)
117 defer_dev_close(.local_envir)
118 }
119
120 #' @describeIn devices TIFF device
121 #' @export
122 with_tiff <- function(new, code, ...) {
123 local_tiff(new, ...)
124 code
125 }
126
127 #' @rdname devices
128 #' @export
129 local_tiff <- function(new, ..., .local_envir = parent.frame()) {
130 grDevices::tiff(new, ...)
131 defer_dev_close(.local_envir)
132 }
133
134 #' @describeIn devices XFIG device
135 #' @export
136 with_xfig <- function(new, code, ...) {
137 local_xfig(new, ...)
138 code
139 }
140
141 #' @rdname devices
142 #' @export
143 local_xfig <- function(new, ..., .local_envir = parent.frame()) {
144 grDevices::xfig(new, ...)
145 defer_dev_close(.local_envir)
146 }
147
148 #' @describeIn devices PNG device
149 #' @export
150 with_png <- function(new, code, ...) {
151 local_png(new, ...)
152 code
153 }
154
155 #' @rdname devices
156 #' @export
157 local_png <- function(new, ..., .local_envir = parent.frame()) {
158 grDevices::png(new, ...)
159 defer_dev_close(.local_envir)
160 }
161
162 #' @describeIn devices JPEG device
163 #' @export
164 with_jpeg <- function(new, code, ...) {
165 local_jpeg(new, ...)
166 code
167 }
168
169 #' @rdname devices
170 #' @export
171 local_jpeg <- function(new, ..., .local_envir = parent.frame()) {
172 grDevices::jpeg(new, ...)
173 defer_dev_close(.local_envir)
174 }
175
176 defer_dev_close <- function(frame) {
177 cur <- grDevices::dev.cur()
178 defer(dev_close(cur), envir = frame)
179
180 # Note that unlike typical `local_` functions we return the current device
181 # rather than the old one
182 invisible(cur)
183 }
184
185 dev_close <- function(which) {
186 prev <- grDevices::dev.prev(which)
187 grDevices::dev.off(which)
188
189 # No devices active
190 if (prev != which) {
191 grDevices::dev.set(prev)
192 }
193
194 prev
195 }