[R] automatically calling dev.off()

axionator axionator at gmail.com
Tue Sep 1 13:34:41 CEST 2015


Hi,
is there a way to automatically call dev.off()?
I use options(device="myfunc") to automatically open a device to print to.
Afterwards, I would like to close it (so that the file is actually written).
I tried to do it via addTaskCallback, but unfortunately, I have to use
Rserve and (for any reason), the callback does not work (in the default R
console on win7 it works, however).

So, the following runs in the R console (but not via Rserve):

mypng <- function(filename = "test.png", ...)
{
    times <- function(total = 2, str = "Task") {
        ctr <- 0
        function(expr, value, ok, visible) {
            ctr <<- ctr + 1
            keep.me <- (ctr < total)
            if (ctr == total) {
                cat("ENDPRINT")
                dev.off()
            }
            keep.me
        }
    }
    png(filename, ...)
    n <- addTaskCallback(times(1))
}
options(device="mypng")
plot(rnorm(333))

Any hints?

	[[alternative HTML version deleted]]



More information about the R-help mailing list