From fda92fe26c0cb5a394cafbbd04e6b17ca0d72321 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Mon, 2 Oct 2023 16:01:08 -0400 Subject: [PATCH 1/2] Add safe.dev.off() function, to avoid oddities found when using dev.off(). --- DESCRIPTION | 2 +- NAMESPACE | 3 ++- NEWS.md | 4 +++- R/bgplot3d.R | 12 ++++++++-- R/plotmath3d.R | 4 ++-- R/zzz.R | 2 +- man/safe.dev.off.Rd | 54 +++++++++++++++++++++++++++++++++++++++++++++ vignettes/rgl.Rmd | 2 +- 8 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 man/safe.dev.off.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 0761a7117..e06ac0564 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: rgl -Version: 1.2.6 +Version: 1.2.7 Title: 3D Visualization Using OpenGL Authors@R: c(person("Duncan", "Murdoch", role = c("aut", "cre"), email = "murdoch.duncan@gmail.com"), diff --git a/NAMESPACE b/NAMESPACE index 730121303..e997c9103 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,6 +45,7 @@ export(.check3d, scale3d, scaleMatrix, scene3d, segments3d, select3d, selectionFunction3d, selectpoints3d, rgl.setAxisCallback, rgl.setMouseCallbacks, rgl.setWheelCallback, + safe.dev.off, set3d, setAxisCallbacks, setGraphicsDelay, setupKnitr, setUserCallbacks, setUserShaders, shade3d, shadow3d, @@ -168,7 +169,7 @@ export(.check3d, importFrom(graphics, legend, par, plot, plot.new, polygon, strwidth, strheight) importFrom(grDevices, as.raster, col2rgb, colorRamp, dev.cur, dev.new, - dev.off, png, postscript, rgb, xy.coords, xyz.coords) + dev.off, dev.prev, dev.set, png, postscript, rgb, xy.coords, xyz.coords) importFrom(stats, approxfun, get_all_vars, model.frame, qchisq, qf, splinefun, terms, var) importFrom(tools, file_ext) diff --git a/NEWS.md b/NEWS.md index c31d15eef..f5fd9ebcd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# rgl 1.2.6 +# rgl 1.2.7 ## Minor changes @@ -12,6 +12,8 @@ run in a Shiny session. `plotmath3d()` (pull request #384). * `polygon3d()` failed when given exactly 3 points (issue #388). * `snapshot3d()` failed on Windows with some versions of `webshot2` (issue #391). +* Fixed issues caused by misuse of `dev.off()` using new function +`safe.dev.off()`. # rgl 1.2.1 diff --git a/R/bgplot3d.R b/R/bgplot3d.R index e6063415c..d27b30369 100644 --- a/R/bgplot3d.R +++ b/R/bgplot3d.R @@ -1,3 +1,11 @@ +safe.dev.off <- function(which = dev.cur(), prev = dev.prev()) { + force(prev) + grDevices::dev.off(which) + dev.set(prev) +} + +dev.off <- function(...) stop("Use safe.dev.off() instead!") + legend3d <- function(...) { args <- list(...) bgargs <- setdiff(names(formals(bgplot3d)), @@ -26,7 +34,7 @@ bgplot3d <- function(expression, bg.color = getr3dDefaults("bg", "color"), bg = bg.color) grDevices::devAskNewPage(FALSE) value <- try(expression) - dev.off() + safe.dev.off() result <- bg3d(texture = filename, col = "white", lit = FALSE, ...) } else { value <- NULL @@ -61,7 +69,7 @@ show2d <- function(expression, filename <- tempfile(fileext = ".png") png(filename = filename, width=width, height=height) value <- try(expression) - dev.off() + safe.dev.off() } else value <- filename face <- c(strsplit(face, '')[[1]], '-')[1:2] diff --git a/R/plotmath3d.R b/R/plotmath3d.R index 9f72e3434..e2ce262e4 100644 --- a/R/plotmath3d.R +++ b/R/plotmath3d.R @@ -33,7 +33,7 @@ plotmath3d <- function(x, y = NULL, z = NULL, w <- strwidth(thistext, cex = initCex, ...) w1 <- strwidth("m", cex = initCex, ...) h <- strheight(thistext, cex = initCex, ...) - dev.off() + safe.dev.off() # Now make a smaller bitmap expand <- 1.5 @@ -44,7 +44,7 @@ plotmath3d <- function(x, y = NULL, z = NULL, usr = c(0, 1, 0, 1)) plot.new() text(0.5, 0.5, thistext, adj = c(0.5,0.5), cex = initCex, ...) - dev.off() + safe.dev.off() # The 0.4 tries to match the text3d offset offseti <- 0.4*offset*h/w posi <- if (is.null(pos)) NULL else pos[i] diff --git a/R/zzz.R b/R/zzz.R index d1469cae5..a43cc96e8 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -115,7 +115,7 @@ !(.Platform$GUI %in% c("AQUA", "RStudio"))) && exists("quartz", getNamespace("grDevices"))) { grDevices::quartz() - dev.off() + safe.dev.off() } ret <- rgl.init(initValue, onlyNULL) diff --git a/man/safe.dev.off.Rd b/man/safe.dev.off.Rd new file mode 100644 index 000000000..d70bccef5 --- /dev/null +++ b/man/safe.dev.off.Rd @@ -0,0 +1,54 @@ +\name{safe.dev.off} +\alias{safe.dev.off} +\title{ +Close graphics device in a safe way. +} +\description{ +The \code{\link{dev.off}} function in \pkg{grDevices} doesn't restore +the previous graphics device when called. This function does. +} +\usage{ +safe.dev.off(which = dev.cur(), prev = dev.prev()) +} +\arguments{ + \item{which}{ +Which device to close. +} + \item{prev}{ +Which device to set as current after closing. +} +} +\value{ +The number and name of the new active device. +} +\references{ +\url{https://bugs.r-project.org/show_bug.cgi?id=18604} +} +\author{ +Duncan Murdoch +} +\examples{ +# Open a graphics device +dev.new() +first <- dev.cur() + +# Open a second graphics device +dev.new() +second <- dev.cur() +second + +# Open another one, and close it using dev.off() +dev.new() +dev.off() +dev.cur() == second # Not the same as second! + +# Try again with safe.dev.off() +dev.set(second) +dev.new() +safe.dev.off() +dev.cur() == second + +# Close the other two devs +safe.dev.off() +safe.dev.off() +} \ No newline at end of file diff --git a/vignettes/rgl.Rmd b/vignettes/rgl.Rmd index 060664c0b..e31958929 100644 --- a/vignettes/rgl.Rmd +++ b/vignettes/rgl.Rmd @@ -374,7 +374,7 @@ because the texture coordinates run from 0 to 2 in both filename <- tempfile(fileext = ".png") png(filename = filename) plot(rnorm(1000), rnorm(1000)) -dev.off() +safe.dev.off() open3d() xyz <- cbind(c(0,1,1,0), 0, c(0,0,1,1)) quads3d(xyz, texture = filename, texcoords = xyz[,c(1, 3)]*2, From 3c69a76c7b722b12ca67d76dcb4170d0f94c4be0 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Mon, 2 Oct 2023 17:54:17 -0400 Subject: [PATCH 2/2] Fix empty devlist; fix pkgdown. --- R/bgplot3d.R | 3 ++- _pkgdown.yml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/R/bgplot3d.R b/R/bgplot3d.R index d27b30369..206465963 100644 --- a/R/bgplot3d.R +++ b/R/bgplot3d.R @@ -1,7 +1,8 @@ safe.dev.off <- function(which = dev.cur(), prev = dev.prev()) { force(prev) grDevices::dev.off(which) - dev.set(prev) + if (length(dev.list())) + dev.set(prev) } dev.off <- function(...) stop("Use safe.dev.off() instead!") diff --git a/_pkgdown.yml b/_pkgdown.yml index 3450477ae..ecdf58888 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -248,6 +248,7 @@ reference: - checkDeldir - Buffer - gltfTypes + - safe.dev.off - title: General documentation - contents: - rgl-package