Colleagues I have a lengthy script that calls mtext. Under most circumstances, a graphics device is open and a plot exists, in which case mtext works as expected. However, there are some instances where the graphics device is open but no plot exists. When mtext is called, I receive an error message: Error in mtext(1) : plot.new has not been called yet The solution is to confirm that: a. the device is open: length(dev.list()) > 0 b. whether plot.new has been called. I need help on the latter - how does one test whether plot.new has been called? Thanks in advance. Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com
On Wed, Apr 28, 2010 at 5:21 PM, Dennis Fisher <fisher at plessthan.com> wrote:> Colleagues > > I have a lengthy script that calls mtext. ?Under most circumstances, a graphics device is open and a plot exists, in which case mtext works as expected. ?However, there are some instances where the graphics device is open but no plot exists. ?When mtext is called, I receive an error message: > ? ? ? ?Error in mtext(1) : plot.new has not been called yet > > The solution is to confirm that: > ? ? ? ?a. ?the device is open: ? ? ? ? length(dev.list()) > 0 > ? ? ? ?b. ?whether plot.new has been called. > > I need help on the latter - how does one test whether plot.new has been called?You could see if par(new=TRUE) generates a warning: > par(new=TRUE) Warning message: In par(new = TRUE) : calling par(new=TRUE) with no plot > plot.new() > par(new=TRUE) > - because the help(par) says: It is an error (ignored with a warning) to try to use ?new=TRUE? on a device that does not currently contain a high-level plot To see if a warning happens, set option(warn=2) to turn warnings to errors, and then wrap the par() call in a try() clause. Or some other way. Or you could just wrap your mtext() call in a try clause.... Barry -- blog: http://geospaced.blogspot.com/ web: http://www.maths.lancs.ac.uk/~rowlings web: http://www.rowlingson.com/ twitter: http://twitter.com/geospacedman pics: http://www.flickr.com/photos/spacedman
On 04/29/2010 02:21 AM, Dennis Fisher wrote:> Colleagues > > I have a lengthy script that calls mtext. Under most circumstances, a graphics device is open and a plot exists, in which case mtext works as expected. However, there are some instances where the graphics device is open but no plot exists. When mtext is called, I receive an error message: > Error in mtext(1) : plot.new has not been called yet > > The solution is to confirm that: > a. the device is open: length(dev.list())> 0 > b. whether plot.new has been called. > > I need help on the latter - how does one test whether plot.new has been called? >Hi Dennis, I use: if(dev.cur() == 1) # there is no graphics device open which always seems to be the null device. Since I have never had occasion to switch to the null device, this is a sort of test for whether there is another graphics device open, and thus whether plot.new has been called (on the current device). While it has always worked for me, I am aware that it is a Sneaky Trick and may not work in some situations. Jim