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