Skip to content

Commit

Permalink
Merge pull request #394 from dmurdoch/devoff
Browse files Browse the repository at this point in the history
Add safe.dev.off() function
  • Loading branch information
dmurdoch committed Oct 2, 2023
2 parents 5565bbc + 3c69a76 commit 1e3b630
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rgl 1.2.6
# rgl 1.2.7

## Minor changes

Expand All @@ -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

Expand Down
13 changes: 11 additions & 2 deletions R/bgplot3d.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
safe.dev.off <- function(which = dev.cur(), prev = dev.prev()) {
force(prev)
grDevices::dev.off(which)
if (length(dev.list()))
dev.set(prev)
}

dev.off <- function(...) stop("Use safe.dev.off() instead!")

legend3d <- function(...) {
args <- list(...)
bgargs <- setdiff(names(formals(bgplot3d)),
Expand Down Expand Up @@ -26,7 +35,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
Expand Down Expand Up @@ -61,7 +70,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]
Expand Down
4 changes: 2 additions & 2 deletions R/plotmath3d.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ reference:
- checkDeldir
- Buffer
- gltfTypes
- safe.dev.off
- title: General documentation
- contents:
- rgl-package
Expand Down
54 changes: 54 additions & 0 deletions man/safe.dev.off.Rd
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion vignettes/rgl.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1e3b630

Please sign in to comment.