From ggrothendieck at gmail.com  Fri Jun  3 14:54:35 2005
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Fri Jun  3 14:55:31 2005
Subject: [Rd] Re: [R] Problem going back to a viewport with gridBase
In-Reply-To: <429FB4AE.5030105@stat.auckland.ac.nz>
References: <971536df05053020191ebe8f44@mail.gmail.com>
	<429CEA59.7020206@stat.auckland.ac.nz>
	<971536df050531163353031d97@mail.gmail.com>
	<429CFA42.4080106@stat.auckland.ac.nz>
	<971536df05053118215366e02c@mail.gmail.com>
	<429E5BFD.9080508@stat.auckland.ac.nz>
	<971536df050602140356cc80b4@mail.gmail.com>
	<429FB4AE.5030105@stat.auckland.ac.nz>
Message-ID: <971536df0506030554396c2374@mail.gmail.com>

On 6/2/05, Paul Murrell <p.murrell@auckland.ac.nz> wrote:
> Hi

Thanks.  I have mucked around in vpTree structures and discovered its
actually quite easy to specify children so I have changed my example
so that instead of naming the children of 'layout' and then remembering 
coordinates linked to the names of the children of 'layout' in 
the 'coords' structure (which really just duplicates state information
already available in grid) it simply follows the order
of the children of 'layout' directly.  This permits elimination of 'coords' 
and the two naming functions.  Using the depth approach you advocate,
'with' also becomes shorter and I think I have it to the point where it works 
with both vpPath and viewport classes.  Once Deepayan implements 
the use.viewport= argument to print, 'with' can be eliminated too.  No 
questions this time but I thought I would post the latest version for
completeness. Regards.

[pushLayout is same as before except there are no names on the
children of 'layout' and the rest is new]

library(grid)
library(lattice)

pushLayout <- function(nr, nc, name="layout") {
  pushViewport(viewport(layout=grid.layout(nr, nc), name=name))
  for (i in 1:nr) {
    for (j in 1:nc) {
      pushViewport(viewport(layout.pos.row=i, layout.pos.col=j))
      upViewport()
    }
  }
  upViewport()
}

with.vpPath <- with.viewport <- function(data, expr, ...) {
      # if data is a vpPath it cannot be ROOT since NULL will never
dispatch here
      depth <- if (data$name == "ROOT") 0 else downViewport(data$name)
      result <- eval.parent(substitute(expr))
      upViewport(depth)
      invisible(result)
}

grid.newpage()

# specify number of cells to fill and number of rows
n <- 5; nr <- 3

nc <- ceiling(n/nr)
downViewport(pushLayout(nr, nc))

vpt <- current.vpTree(all = FALSE)
for(k in 1:n) with(vpt$children[[k]],
      print( xyplot(v ~ v, list(v = 1:k)), newpage = FALSE )
)


[Rd] Re: [R] Problem going back to a viewport with gridBase
Gabor Grothendieck