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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed 08 Jun 2005 - 01:57:14 GMT

Here is the code once again. This time I have supplied two names methods and a getChildren.viewport function to encapsulate the corresponding grid internals. It would be easiest if grid provided these itself but in the absence of that this does encapsulate dependencies on grid internals to a well defined set of functions. Note that names is only used in 'with' and 'with' will be eliminated once Deepayan adds the use.viewport= (or whatever its called) to print. I am not sure from your response whether or not you intend to add these items to the grid API but in any case this provides an intermediate level of safety.

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()
}

names.vpPath <- names.viewport <- function(x) x$name

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(names(data))
      result <- eval.parent(substitute(expr))
      upViewport(depth)
      invisible(result)

}

getChildren.viewport <- function(x) x$children

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(getChildren.viewport(vpt)[[k]],

      print( xyplot(v ~ v, list(v = 1:k)), newpage = FALSE ) )



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Jun 08 12:00:03 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:08 GMT