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

Jim Lemon jim at bitwrit.com.au
Thu Apr 29 12:13:41 CEST 2010


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



More information about the R-help mailing list