Rev 30741 | Blame | Last modification | View Log | Download | RSS feed
\documentclass[a4paper]{article}%\VignetteIndexEntry{Persistent representations}%\VignettePackage{grid}\newcommand{\grid}{{\tt grid}}\newcommand{\R}{{\tt R}}\setlength{\parindent}{0in}\setlength{\parskip}{.1in}\setlength{\textwidth}{140mm}\setlength{\oddsidemargin}{10mm}\title{Saving and Loading \grid{} Graphics}\author{Paul Murrell}\begin{document}\maketitle<<echo=FALSE, results=hide>>=library(grid)ps.options(pointsize=12)options(width=60)@This is a general discussion concerning how you might go aboutcreating (and reusing) persistent representations of \grid{}graphics, and some of the pitfalls in the various options.\subsection*{\R{} code}The way I usually work with graphics is to write \R{} code in a text fileand copy-and-paste or {\tt source()} it into R. In this case,the presistentrepresentation of the graphics is the raw \R{} code.The representation is fully editable.The representation is persistent across \R{} sessions, but may not bepersistent across \R{} versions because the names, argument lists,and/or behaviour of the \grid{} functions maychange. The representation can be reloaded into R.Incompatibilities between versions should be handled gracefully by\R{}'s argument-matching, type-checking, and/or version-checking.\subsection*{\grid{} {\tt grob}s}The next most flexibleway of creating a persistent versionof \grid{} graphics is to {\tt save()} a \grid{} {\tt grob} ...<<>>=gt <- textGrob("hi")save(gt, file="mygridplot")@The representation is reloadable so you can reproduce an image ...<<results=hide>>=load("mygridplot")grid.draw(gt)@And the representation is editable; there is an API for interactingwith \grid{} {\tt grobs}, including adding new elements, removingelements, editing features of a {\tt grob} and so on. See{\tt getGrob()}, {\tt addGrob()}, {\tt removeGrob()},and {\tt editGrob()}.The representation is persistent across \R{} sessions, but isvulnerable to changes in the internal representation of \grid{}{\tt grob}s. The advantage of this representation is that it ispossible to share and edit graphics produced by someone else,without seeing the code they used to produce it.\subsection*{Device output}A third way of creating a persistent version of \grid{} graphics is to``save'' it to a persistent device format (e.g., PostScript, PDF, ...).It is possible to edit these representations, but hardly convenient.In particular, none of the coordinate systems used in theconstruction of plots (e.g., axis scales) are available for editing.The representation is persistent regardless of \R{} sessions or versions,but it cannot be reloaded into \R{}.\subsection*{Display lists}This final option is \emph{not} recommended, but it is possible to doso its downsides need to be explained.What you can do is``save'' an \R{} display list using, for example, ...<<>>=grid.grill()temp <- recordPlot()save(temp, file="mygridplot")@This representation is not editable\footnote{Well, there's nothing stoppingyou editing it, but you should take out life insurance first.}, but it can bereloadedand rerun to reproduce the output ...<<>>=load("mygridplot")temp@The representation is persistent across \R{} sessions,but the saved information involves non-public interfaces and structureswhich may change between \R{} versions. You alsohaveto make sure that \grid{} has been reloaded. Differences between\grid{} and/or \R{}versions may lead to segmentation faults.% Start a new page% Not echoed, not evaluated% ONLY here for checkVignettes so that all output doesn't% end up on one enormous page<<eval=FALSE, echo=FALSE>>=grid.newpage()@\end{document}