From 74dcbdb78ab8d0318cc0e359da971f82d2415e34 Mon Sep 17 00:00:00 2001 From: coryadkins Date: Thu, 17 Sep 2026 14:43:50 -0500 Subject: [PATCH] Fixes #244 - local device functions default to use temporary local file --- DESCRIPTION | 2 +- R/devices.R | 80 ++++++++++++++++++++++++++++++++++----- man/devices.Rd | 32 ++++++++-------- man/with_collate.Rd | 2 +- man/with_libpaths.Rd | 4 +- man/with_temp_libpaths.Rd | 4 +- man/withr.Rd | 1 + 7 files changed, 93 insertions(+), 32 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2b72030b..6790af44 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,7 +39,6 @@ Config/Needs/website: tidyverse/tidytemplate Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 Collate: 'aaa.R' 'collate.R' @@ -71,3 +70,4 @@ Collate: 'torture.R' 'utils.R' 'with.R' +Config/roxygen2/version: 8.1.0 diff --git a/R/devices.R b/R/devices.R index 1b1b9941..adcd5b3e 100644 --- a/R/devices.R +++ b/R/devices.R @@ -42,7 +42,13 @@ with_bmp <- function(new, code, ...) { #' @rdname devices #' @export -local_bmp <- function(new, ..., .local_envir = parent.frame()) { +local_bmp <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".bmp" + ) grDevices::bmp(new, ...) defer_dev_close(.local_envir) } @@ -56,7 +62,13 @@ with_cairo_pdf <- function(new, code, ...) { #' @rdname devices #' @export -local_cairo_pdf <- function(new, ..., .local_envir = parent.frame()) { +local_cairo_pdf <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".pdf" + ) grDevices::cairo_pdf(new, ...) defer_dev_close(.local_envir) } @@ -70,7 +82,13 @@ with_cairo_ps <- function(new, code, ...) { #' @rdname devices #' @export -local_cairo_ps <- function(new, ..., .local_envir = parent.frame()) { +local_cairo_ps <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".ps" + ) grDevices::cairo_ps(new, ...) defer_dev_close(.local_envir) } @@ -84,7 +102,13 @@ with_pdf <- function(new, code, ...) { #' @rdname devices #' @export -local_pdf <- function(new, ..., .local_envir = parent.frame()) { +local_pdf <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".pdf" + ) grDevices::pdf(new, ...) defer_dev_close(.local_envir) } @@ -98,7 +122,13 @@ with_postscript <- function(new, code, ...) { #' @rdname devices #' @export -local_postscript <- function(new, ..., .local_envir = parent.frame()) { +local_postscript <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".ps" + ) grDevices::postscript(new, ...) defer_dev_close(.local_envir) } @@ -112,7 +142,13 @@ with_svg <- function(new, code, ...) { #' @rdname devices #' @export -local_svg <- function(new, ..., .local_envir = parent.frame()) { +local_svg <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".svg" + ) grDevices::svg(new, ...) defer_dev_close(.local_envir) } @@ -126,7 +162,13 @@ with_tiff <- function(new, code, ...) { #' @rdname devices #' @export -local_tiff <- function(new, ..., .local_envir = parent.frame()) { +local_tiff <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".tif" + ) grDevices::tiff(new, ...) defer_dev_close(.local_envir) } @@ -140,7 +182,13 @@ with_xfig <- function(new, code, ...) { #' @rdname devices #' @export -local_xfig <- function(new, ..., .local_envir = parent.frame()) { +local_xfig <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".fig" + ) grDevices::xfig(new, ...) defer_dev_close(.local_envir) } @@ -154,7 +202,13 @@ with_png <- function(new, code, ...) { #' @rdname devices #' @export -local_png <- function(new, ..., .local_envir = parent.frame()) { +local_png <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".png" + ) grDevices::png(new, ...) defer_dev_close(.local_envir) } @@ -168,7 +222,13 @@ with_jpeg <- function(new, code, ...) { #' @rdname devices #' @export -local_jpeg <- function(new, ..., .local_envir = parent.frame()) { +local_jpeg <- function(new = NULL, ..., .local_envir = parent.frame()) { + new <- new %||% + local_tempfile( + .local_envir = .local_envir, + pattern = "withr_file", + fileext = ".jpg" + ) grDevices::jpeg(new, ...) defer_dev_close(.local_envir) } diff --git a/man/devices.Rd b/man/devices.Rd index a2fd84be..030fab3f 100644 --- a/man/devices.Rd +++ b/man/devices.Rd @@ -28,43 +28,43 @@ \usage{ with_bmp(new, code, ...) -local_bmp(new, ..., .local_envir = parent.frame()) +local_bmp(new = NULL, ..., .local_envir = parent.frame()) with_cairo_pdf(new, code, ...) -local_cairo_pdf(new, ..., .local_envir = parent.frame()) +local_cairo_pdf(new = NULL, ..., .local_envir = parent.frame()) with_cairo_ps(new, code, ...) -local_cairo_ps(new, ..., .local_envir = parent.frame()) +local_cairo_ps(new = NULL, ..., .local_envir = parent.frame()) with_pdf(new, code, ...) -local_pdf(new, ..., .local_envir = parent.frame()) +local_pdf(new = NULL, ..., .local_envir = parent.frame()) with_postscript(new, code, ...) -local_postscript(new, ..., .local_envir = parent.frame()) +local_postscript(new = NULL, ..., .local_envir = parent.frame()) with_svg(new, code, ...) -local_svg(new, ..., .local_envir = parent.frame()) +local_svg(new = NULL, ..., .local_envir = parent.frame()) with_tiff(new, code, ...) -local_tiff(new, ..., .local_envir = parent.frame()) +local_tiff(new = NULL, ..., .local_envir = parent.frame()) with_xfig(new, code, ...) -local_xfig(new, ..., .local_envir = parent.frame()) +local_xfig(new = NULL, ..., .local_envir = parent.frame()) with_png(new, code, ...) -local_png(new, ..., .local_envir = parent.frame()) +local_png(new = NULL, ..., .local_envir = parent.frame()) with_jpeg(new, code, ...) -local_jpeg(new, ..., .local_envir = parent.frame()) +local_jpeg(new = NULL, ..., .local_envir = parent.frame()) } \arguments{ \item{new}{\code{[named character]}\cr New graphics device} @@ -84,16 +84,16 @@ Temporarily use a graphics device. } \details{ \itemize{ -\item \code{with_bmp()} and \code{local_bmp()} wrap around \code{\link[grDevices:png]{grDevices::bmp()}}. -\item \code{with_cairo_pdf()} and \code{local_cairo_pdf()} wrap around \code{\link[grDevices:cairo]{grDevices::cairo_pdf()}}. -\item \code{with_cairo_ps()} and \code{local_cairo_ps()} wrap around \code{\link[grDevices:cairo]{grDevices::cairo_ps()}}. +\item \code{with_bmp()} and \code{local_bmp()} wrap around \code{\link[grDevices:bmp]{grDevices::bmp()}}. +\item \code{with_cairo_pdf()} and \code{local_cairo_pdf()} wrap around \code{\link[grDevices:cairo_pdf]{grDevices::cairo_pdf()}}. +\item \code{with_cairo_ps()} and \code{local_cairo_ps()} wrap around \code{\link[grDevices:cairo_ps]{grDevices::cairo_ps()}}. \item \code{with_pdf()} and \code{local_pdf()} wrap around \code{\link[grDevices:pdf]{grDevices::pdf()}}. \item \code{with_postscript()} and \code{local_postscript()} wrap around \code{\link[grDevices:postscript]{grDevices::postscript()}}. -\item \code{with_svg()} and \code{local_svg()} wrap around \code{\link[grDevices:cairo]{grDevices::svg()}}. -\item \code{with_tiff()} and \code{local_tiff()} wrap around \code{\link[grDevices:png]{grDevices::tiff()}}. +\item \code{with_svg()} and \code{local_svg()} wrap around \code{\link[grDevices:svg]{grDevices::svg()}}. +\item \code{with_tiff()} and \code{local_tiff()} wrap around \code{\link[grDevices:tiff]{grDevices::tiff()}}. \item \code{with_xfig()} and \code{local_xfig()} wrap around \code{\link[grDevices:xfig]{grDevices::xfig()}}. \item \code{with_png()} and \code{local_png()} wrap around \code{\link[grDevices:png]{grDevices::png()}}. -\item \code{with_jpeg()} and \code{local_jpeg()} wrap around \code{\link[grDevices:png]{grDevices::jpeg()}}. +\item \code{with_jpeg()} and \code{local_jpeg()} wrap around \code{\link[grDevices:jpeg]{grDevices::jpeg()}}. } } \section{Functions}{ diff --git a/man/with_collate.Rd b/man/with_collate.Rd index 222fd63e..858b6ee5 100644 --- a/man/with_collate.Rd +++ b/man/with_collate.Rd @@ -31,7 +31,7 @@ Temporarily change collation order by changing the value of the x <- c("bernard", "bérénice", "béatrice", "boris") with_collate("fr_FR", sort(x)) -#> [1] "béatrice" "bérénice" "bernard" "boris" +#> [1] "béatrice" "bérénice" "bernard" "boris" with_collate("C", sort(x)) #> [1] "bernard" "boris" "béatrice" "bérénice" diff --git a/man/with_libpaths.Rd b/man/with_libpaths.Rd index 652fbd8e..8413ac0d 100644 --- a/man/with_libpaths.Rd +++ b/man/with_libpaths.Rd @@ -38,7 +38,7 @@ unlink(new_lib, recursive = TRUE) \code{\link[=.libPaths]{.libPaths()}} -Other libpaths: -\code{\link{with_temp_libpaths}()} +Other libpaths: +\code{\link[=with_temp_libpaths]{with_temp_libpaths()}} } \concept{libpaths} diff --git a/man/with_temp_libpaths.Rd b/man/with_temp_libpaths.Rd index f40c9aab..9bd6b7a6 100644 --- a/man/with_temp_libpaths.Rd +++ b/man/with_temp_libpaths.Rd @@ -29,7 +29,7 @@ Temporarily prepend a new temporary directory to the library paths. \code{\link[=.libPaths]{.libPaths()}} -Other libpaths: -\code{\link{with_libpaths}()} +Other libpaths: +\code{\link[=with_libpaths]{with_libpaths()}} } \concept{libpaths} diff --git a/man/withr.Rd b/man/withr.Rd index 2fdd4ff0..2a81bc9b 100644 --- a/man/withr.Rd +++ b/man/withr.Rd @@ -89,6 +89,7 @@ Useful links: Authors: \itemize{ + \item Lionel Henry \email{lionel@posit.co} \item Jim Hester \item Kirill Müller \email{krlmlr+r@mailbox.org} \item Kevin Ushey \email{kevinushey@gmail.com}