Hi
I could speculate, but what would be more useful would be some profiling
results. If you could try Rprof() on your examples (and post me the
results directly), that would provide some useful information to see if
some speed-ups could be made.
Paul
baptiste auguie wrote:
Hi,
I just tried a fourth variant, closer to what ggplot2 uses (I think):
to each grob is assigned a viewport with row and column positions (in
my example during their construction, with ggplot2 upon editing), and
they're all plotted in a given grid.layout. The timing is poor
compared to pushing and upping viewports (twice as long).
Why would that be?
All the best,
baptiste
(the full, self-contained comparison file is attached, run as:
R --vanilla -f comparison.r )
# below is version 4 only
makeContentInVp <- function(d){
content <- as.character(unlist(c(d)))
nc <- ncol(d)
nr <- nrow(d)
n2nm <- function(nr, nc){
expand.grid(seq(1, nr), seq(1, nc))
}
vp.ind <- n2nm(nr, nc)
textii <- function(d, gp=gpar(), name="content-label-"){
function(ii)
textGrob(label=d[ii], gp=gp,
name=paste(name, ii, sep=""),
vp=viewport(layout.pos.row=vp.ind[ii, 1],
layout.pos.col=vp.ind[ii, 2]))
}
makeOneLabel <- textii(d=content, gp=gpar(col="blue"))
lg <- lapply(seq_along(content), makeOneLabel)
list(lg=lg, nrow=nrow(d), ncol=ncol(d))
}
## table4 uses grobs that already have a viewport assigned
table4 <- function(content){
padding <- unit(4, "mm")
lg <- content$lg
## retrieve the widths and heights of all textGrobs
wg <- lapply(lg, grobWidth) # list of grob widths
hg <- lapply(lg, grobHeight) # list of grob heights
## concatenate this units
widths.all <- do.call(unit.c, wg) # all grob widths
heights.all <- do.call(unit.c, hg) #all grob heights
## matrix-like operations on units to define the table layout
widths <- colMax.units(widths.all, content$ncol) # all column widths
heights <- rowMax.units(heights.all, content$nrow) # all row heights
vp <- viewport(layout=grid.layout(content$nrow,content$ncol,
w=widths+padding, h=heights+padding))
grid.draw(gTree(children=do.call(gList, lg), vp=vp))
}
# uncomment for timing
d <- head(iris)
#d <- iris
content2 <- makeContentInVp(d)
# grid.newpage()
# system.time(table3(content))
## user system elapsed
## 4.422 0.091 4.787
grid.newpage()
system.time(table4(content2))
## user system elapsed
## 8.810 0.184 9.555
2009/9/25 hadley wickham <h.wick...@gmail.com>:
This matches my experience with ggplot2 - I have been gradually moving
away from frameGrob and packGrob because doing the placement myself is
much faster (and for most of the cases I'm interested in, the full
power of packGrob is not needed)
Hadley
--
http://had.co.nz/
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
p...@stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/
______________________________________________
R-help@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.