safe.dev.off.Rd
The dev.off
function in grDevices doesn't restore
the previous graphics device when called. This function does.
This function closes device which
if it is not device 1,
then calls dev.set(prev)
if there are any devices still
open.
The number and name of the new active device. It will not
necessarily be prev
if that device isn't already open.
# Open a graphics device
dev.new()
first <- dev.cur()
# Open a second graphics device
dev.new()
second <- dev.cur()
second
#> agg_png
#> 6
# Open another one, and close it using dev.off()
dev.new()
dev.off()
#> RStudioGD
#> 2
dev.cur() == second # Not the same as second!
#> pdf
#> FALSE
# Try again with safe.dev.off()
dev.set(second)
#> agg_png
#> 6
dev.new()
safe.dev.off()
#> agg_png
#> 6
dev.cur() == second
#> pdf
#> FALSE
# Close the other two devs
safe.dev.off()
#> agg_png
#> 3
safe.dev.off()
#> agg_png
#> 3