Some time ago I posted a question in stackoverflow to learn how to produce a multipage SVG file. This is easy if you want a PDF:
trellis.device(pdf, file='iris.pdf') p <- xyplot(Sepal.Length~Petal.Length|Species, data=iris, layout=c(1, 1)) print(p) dev.off()
But the PDF file is not really useful to get something similar to a slideshow. There wasn’t a definitive answer but the best comment was: “to make R generate a bunch of SVGs and then manually make some html page with javaScript doing the page switching”. I finally decided to ask directy to Paul Murrell, the creator of grid and gridSVG (recently available at CRAN). He kindly provided some good advices and code which completely solved the problem with an approach similar to the advice from stackoverflow. I modified his code to write the function animateTrellis:
library(gridSVG)
library(XML)
animateTrellis <- function(object, file='animatedSVG.svg',
duration=.1, step=2, show=TRUE){
nLayers <- dim(object)
stopifnot(nLayers>1)
for (i in seq_len(nLayers)){
p <- object[i]
label <- p$condlevels[[1]][i]
##Create intermediate SVG files
g <- grid.grabExpr(print(p, prefix=label),
name=label)
if (i==1){ ## First frame
ga <- animateGrob(g, group=TRUE,
visibility="hidden",
duration=duration, begin=step)
} else if (i==nLayers){ ##Last Frame
gg <- garnishGrob(g, visibility='hidden')
ga <- animateGrob(gg, group=TRUE,
visibility="visible",
duration=duration, begin=step*(i-1))
} else { ##any frame
gg <- garnishGrob(g, visibility='hidden')
gaV <- animateGrob(gg, group=TRUE,
visibility="visible",
duration=duration, begin=step*(i-1))
ga <- animateGrob(gaV, group=TRUE,
visibility="hidden",
duration=duration, begin=step*i)
}
grid.newpage()
grid.draw(ga)
fich <- tempfile(fileext='.svg')
gridToSVG(fich)
## Combine all
if (i==1) {
svgTop <- xmlParse(fich)
nodeTop <- getNodeSet(svgTop,
"//svg:g[@id='gridSVG']",
c(svg="http://www.w3.org/2000/svg"))[[1]]
} else {
svgChildren <- xmlParse(fich)
node <- getNodeSet(svgChildren,
"//svg:g[@id='gridSVG']/*",
c(svg="http://www.w3.org/2000/svg"))
addChildren(nodeTop, node)
}
unlink(fich)
}
saveXML(svgTop, file=file)
dev.off()
if (show) browseURL(file)
invisible(svgTop)
}
Now you can produce a SVG file with animation from the previous trellis object:
animateTrellis(p, file='iris.svg')
You will find lot of information and examples of gridSVG in this article.
F. Tusell dijo:
Gracias por tu artículo. Tratando de reproducir tu ejemplo, obtengo:
> animateTrellis(p, file=’iris.svg’)
Error en .Call(“GD_nullDevice”, PACKAGES = “grDevices”) :
C nombre de símbolo “GD_nullDevice” no está en la tabla de carga
Empleo R 2.14.0 compilado en Linux 32bits. ¿Es posible que falte alguna librería?
Un saludo, ft.
Oscar Perpiñán Lamigueiro dijo:
Parece que el problema está en
grid.grabExpr. Es como si en R2.14.0 hubiese cambios engrDevicessin su correspondencia engrid.Por lo que he podido ver, la versión de desarrollo (grid.R) ya incorpora cambios en
grid.grabExprque hacen que vuelva a funcionar. Si utilizas este trozo de código antes deanimateTrellisobtendrás resultados correctos:grid.grabExpr <- function(expr, warn=2, wrap=FALSE, …) {
# Start an "offline" PDF device for this function
# .Call("R_GD_nullDevice", PACKAGE = "grDevices")
pdf(file=NULL)
on.exit(dev.off())
# Run the graphics code in expr
# Rely on lazy evaluation for correct "timing"
eval(expr)
# Grab the DL on the new device
grid:::grabDL(warn, wrap, …)
}
F. Tusell dijo:
Lo probaré en cuanto pueda. Muchas gracias de nuevo.