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]]
Don't use options to do this in batch mode. Open and close the file as you make the plot. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On September 1, 2015 4:34:41 AM PDT, axionator <axionator at gmail.com> wrote:>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]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
I did it on the console only for testing. I have a program that uses Rserve and would like to have it therefore. (the program basically provides a terminal where you can type in R commands. Since plotting is only possible via files (when using Rserve) that are then displayed in a widget, I like to avoid the overhead of writing png() ... dev.off() for each plot.) On Tue, Sep 1, 2015 at 3:42 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> Don't use options to do this in batch mode. Open and close the file as you > make the plot. > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live > Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > On September 1, 2015 4:34:41 AM PDT, axionator <axionator at gmail.com> > wrote: > >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]] > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
On 01/09/2015 7:34 AM, axionator wrote:> 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)) >It wouldn't make sense to call dev.off() after the plot() command, because the user might be planning to add something to it. You need to tell R that you are done, and that's what dev.off() is for. You can call it automatically in a function by using on.exit(dev.off()) and it should happen automatically at the end of an R session, but it *shouldn't* happen after every plotting call. Duncan Murdoch
Yes, the user could add something. Here, In about 90% of the time, he won't. So it would be nice to have a dev.off() as default (and some extra handling if more plot commands will follow). On Tue, Sep 1, 2015 at 4:04 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 01/09/2015 7:34 AM, axionator wrote: > > 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)) > > > > It wouldn't make sense to call dev.off() after the plot() command, > because the user might be planning to add something to it. You need to > tell R that you are done, and that's what dev.off() is for. > > You can call it automatically in a function by using > > on.exit(dev.off()) > > and it should happen automatically at the end of an R session, but it > *shouldn't* happen after every plotting call. > > Duncan Murdoch > >[[alternative HTML version deleted]]