Skip to content
Prev 131540 / 398502 Next

if/else for plot/lines?

On Fri, 7 Dec 2007, Roger Levy wrote:

            
exists() will not help with graphics devices, whose state is not stored in 
R objects that exists() can test for.

Note that it is rare for a graphics device to be open and not contain a 
plot.  But in those circumstances par("usr") will be c(0,1,0,1) which 
would be unusual after a plot.

A simple way to test if a non-null device is active is dev.cur() > 1.

However, I doubt if you want to add lines to any old plot that happens to 
be on the device, and there is no general way to tell if the existing 
plot is suitable (it need not be the last plot made, for example).
So I can only see this goal as achievable within a constrained set of 
circumstances.

(A further complication is that a graphics device can display either base 
or grid graphics, and you can't add base graphics to a grid plot or v.v.)