[R] Determining whether "plot.new" has been called

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Apr 28 18:39:19 CEST 2010


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



More information about the R-help mailing list