[R] viewport bug in 2.8.0?: Error: Cannot pop the top-level viewport (grid and graphics output mixed?)

Paul Murrell p.murrell at auckland.ac.nz
Tue Mar 31 21:52:02 CEST 2009


Hi


jonathan.shore at hsbcib.com wrote:
> I get the following error message followed by instability in R after the 
> error message:
> 
>         Error: Cannot pop the top-level viewport (grid and graphics output 
> mixed?)
> 
> Have reduced something much more complex in my code to a simple test case. 
>   I run the following and then resize the window:
> 
> X = seq (1,10)
> Y = X^2
> 
> opar <- par(no.readonly=TRUE, mar = c(2.5, 3.1, 1, 2))
> grid.newpage()
> 
> pushViewport (viewport(x=0,y=0,width=1,height=1,just=c(0,0), name='base'))
> pushViewport (viewport(x=0,y=0.5,width=1,height=0.5,just=c(0,0)))
> par (fig=gridFIG())
> 
> # plot in top half of page
> plot (X,Y,col='black',xlab="",main='',cex.axis=.7)
> 
> # get toplevel view
> seekViewport('base')
> 
> # create lower viewport
> pushViewport(viewport(x=0,y=0,width=1, height=0.5, just=c(0,0))) 
> par(new=TRUE,fig=gridFIG()) 
> 
> plot (X,Y,col='grey')
> 
> Upon resize will display the above error.   R will then become unstable 
> and I have to terminate.    This is in the windows version of R 2.8.0.
> 
> Am I doing something wrong?   I am using viewports as I am doing something 
> more complex than the above in code with a number of additional viewports 
> (In this case I could use
> layout or split.screen)


The combination of grid and traditional graphics is an uncomfortable
(unholy?) alliance.  One particularly sensitive point occurs when
starting a new page because traditional graphics tends to think it is
the only game in town and completely wipes the graphics device clean.
In your example above, this means that the first call to plot() removes
all memory of the grid viewports that were pushed before it (this is
what is generating the error message when you resize the window).  A
workaround is possible because grid graphics is a bit more accepting of
others and will notice if there has already been drawing on the graphics
device, so the generally safer approach is to *start* a new page using
traditional graphics, then add grid and other traditional graphics to
the page.  A modification of your example using this strategy is shown
below ...

opar <- par(no.readonly=TRUE, mar = c(2.5, 3.1, 1, 2))
# CHANGE
# Start new page with traditional graphics
plot.new()

pushViewport(viewport(x=0,y=0,width=1,height=1,
                      just=c(0,0), name='base'))
pushViewport (viewport(x=0,y=0.5,width=1,height=0.5,just=c(0,0)))
# CHANGE
par (new=TRUE, fig=gridFIG())

# plot in top half of page
plot (X,Y,col='black',xlab="",main='',cex.axis=.7)

# get toplevel view
seekViewport('base')

# create lower viewport
pushViewport(viewport(x=0,y=0,width=1, height=0.5, just=c(0,0)))
par(new=TRUE,fig=gridFIG())

plot (X,Y,col='grey')

... does something similar work for your "real" code ?

(the instability is another issue, but I don't get that on Linux, so
it's going to be harder to track down)

Paul


> regards
> 
> Jonathan Shore
> 
> 
> 
> -----------------------------------------
> SAVE PAPER - THINK BEFORE YOU PRINT!
> 
> This transmission has been issued by a member of the HSBC Group
> "HSBC" for the information of the addressee only and should not be
> reproduced and/or distributed to any other person. Each page
> attached hereto must be read in conjunction with any disclaimer
> which forms part of it. Unless otherwise stated, this transmission
> is neither an offer nor the solicitation of an offer to sell or
> purchase any investment. Its contents are based on information
> obtained from sources believed to be reliable but HSBC makes no
> representation and accepts no responsibility or liability as to its
> completeness or accuracy.
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list