From p.murrell at auckland.ac.nz  Thu Jan 24 03:38:25 2008
From: p.murrell at auckland.ac.nz (Paul Murrell)
Date: Thu, 24 Jan 2008 15:38:25 +1300
Subject: [Rd] seekViewport error
In-Reply-To: <971536df0712261026w6bdebe49re08ed45e28af8873@mail.gmail.com>
References: <971536df0712261026w6bdebe49re08ed45e28af8873@mail.gmail.com>
Message-ID: <4797FA21.3090700@stat.auckland.ac.nz>

Hi


Gabor Grothendieck wrote:
> Why does the seekViewport at the bottom give an error?


Because the viewport is popped after GRID.cellGrob.84 is drawn.

grid.ls() shows the viewport because it recurses down into the legend 
frame grob.  Compare your output with (grid-generated numbering differs) 
  ...

 > grid.ls(recurs=FALSE, view=TRUE)
ROOT
   GRID.rect.28
   plot1.toplevel.vp
     plot1.xlab.vp
       plot1.xlab
       1
     plot1.ylab.vp
       plot1.ylab
       1
     plot1.strip.1.1.off.vp
       GRID.segments.29
       1
     plot1.strip.left.1.1.off.vp
       GRID.segments.30
       GRID.text.31
       1
     plot1.panel.1.1.off.vp
       GRID.segments.32
       GRID.text.33
       GRID.segments.34
       1
     plot1.panel.1.1.vp
       GRID.points.35
       GRID.points.36
       GRID.points.37
       1
     plot1.panel.1.1.off.vp
       GRID.rect.38
       1
     plot1.legend.top.vp
       GRID.frame.9
       1
     plot1.
       1
     1

If you look at what viewports are actually available, via 
current.vpTree(), you'll see that GRID.VP.24 is not there.

The problem (see also 
https://stat.ethz.ch/pipermail/r-help/2008-January/151655.html) is that 
cellGrobs (children of frame grobs) use their 'vp' component to store 
the viewport that positions them within the parent frame.  This means 
that the viewport is pushed and then popped (as per normal behaviour for 
'vp' components).

A possible solution that I am currently trialling uses a special 
'cellvp' slot instead so that the cellGrob viewports are pushed and then 
"upped".  That way they remain available after the cellGrob has drawn, 
so you can downViewport() to them.

The disadvantage of this approach is that the viewports no longer appear 
in the grid.ls() listing (because grid.ls() has no way of knowing about 
special components of grobs that contain viewports).  This effect can 
already be seen by the fact that the viewport for the frame grob 
(GRID.frame.70) is not shown in the grid.ls() output.  On the other 
hand, the viewports will be visible via current.vpTree()  ...

 > current.vpTree()
viewport[ROOT]->(viewport[GRID.VP.1], 
viewport[plot1.toplevel.vp]->(viewport[plot1.], 
viewport[plot1.legend.top.vp]->(viewport[GRID.VP.1]->(viewport[GRID.VP.2], 
viewport[GRID.VP.3], viewport[GRID.VP.4], viewport[GRID.VP.5], 
viewport[GRID.VP.6], viewport[GRID.VP.7], viewport[GRID.VP.8])), 
viewport[plot1.panel.1.1.off.vp], viewport[plot1.panel.1.1.vp], 
viewport[plot1.strip.1.1.off.vp], viewport[plot1.strip.left.1.1.off.vp], 
viewport[plot1.xlab.vp], viewport[plot1.ylab.vp]))

... (in the output above, the grid-generated viewport numbers differ, 
but you can see the idea;  I think GRID.VP.8 corresponds to your GRID.VP.24)

Paul


>> xyplot(Sepal.Length ~ Sepal.Width, iris, group = Species, col = 11:13,
> +   auto.key = TRUE)
>> grid.ls(view = TRUE)
> ROOT
>   GRID.rect.89
>   plot1.toplevel.vp
>     plot1.xlab.vp
>       plot1.xlab
>       1
>     plot1.ylab.vp
>       plot1.ylab
>       1
>     plot1.strip.1.1.off.vp
>       GRID.segments.90
>       1
>     plot1.strip.left.1.1.off.vp
>       GRID.segments.91
>       GRID.text.92
>       1
>     plot1.panel.1.1.off.vp
>       GRID.segments.93
>       GRID.text.94
>       GRID.segments.95
>       1
>     plot1.panel.1.1.vp
>       GRID.points.96
>       GRID.points.97
>       GRID.points.98
>       1
>     plot1.panel.1.1.off.vp
>       GRID.rect.99
>       1
>     plot1.legend.top.vp
>       GRID.frame.70
>         GRID.VP.18
>           GRID.cellGrob.72
>             GRID.rect.71
>           1
>         GRID.VP.19
>           GRID.cellGrob.74
>             GRID.text.73
>           1
>         GRID.VP.20
>           GRID.cellGrob.76GRID.cellGrob.84
>             GRID.text.75
>           1
>         GRID.VP.21
>           GRID.cellGrob.78
>             GRID.text.77
>           1
>         GRID.VP.22
>           GRID.cellGrob.80
>             GRID.points.79
>           1
>         GRID.VP.23
>           GRID.cellGrob.82
>             GRID.points.81
>           1
>         GRID.VP.24
>           GRID.cellGrob.84
>             GRID.points.83
>           1
>       1
>     plot1.
>       1
>     1
>> seekViewport("GRID.VP.24")
> Error in downViewport.vpPath(vpPathDirect(name), strict, recording =
> recording) :
>   Viewport 'GRID.VP.24' was not found
> 
>> R.version.string # Vista
> [1] "R version 2.6.1 Patched (2007-12-06 r43610)"
>> packageDescription("grid")$Version
> [1] "2.6.1"
>> packageDescription("lattice")$Version
> [1] "0.17-2"
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
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/



[Rd] seekViewport error
Paul Murrell