Looping through a set of graphics in odfWeave

November 3, 2010
By

(This article was first published on socialdatablog » R, and kindly contributed to R-bloggers)

At proMENTE social research we often use the odfWeave and Sweave
packages for the amazing statistics program R for automating the
production of graphics and reports. odfWeave and Sweave are for the
OpenOffice and lyx (www.lyx.org) word processors respectively.

One problem with this approach arises when you have to produce and
insert into your document a whole bunch of graphics. For any other bunch
of things such as tables it is easy enough to make different kinds of
loops to produce the sets of tables you want. But graphics are a bit
more tricky because each graphic requires its own frame.

One way to do this which we have developed in odfWeave is to write a
loop to produce an intermediate source document which contains the code
for producing or inserting the actual graphics. Then you run odfWeave on
this intermediate document to produce the final document with the
graphics. So you do your weaving twice.

Assume you have a dataframe df of variables. Assume you have already
produced a folder full of graphics, named with the colnames of the
dataframe (if you have got this far, you are versed enough to know how
to do this already).

We will use the following function in the source document, source.odt,
to loop through the dataframe and produce an intermediate document
called intermediate.odt with one piece of odfInsertPlot code for each
graphic. Then the second odfWeave command produces the final document
from intermediate.odt.

This is the function you need to have defined previously

xodf.t=function(x) cat("<>=","n", 
"cat(odfInsertPlot('/path/to/file/",x,".png',externalFile=T))","n", 
"odfCat('some caption')","n", "@","n",sep="")

And this is the loop you need to put in the source.doc.

<>=
for(x in colnames(df)) xodf.t(x)
@

So you run odfWeave(“source.odt”,“intermediate.odt”) in R and then run
odfWeave(“intermediate.odt”,“final.odt”) again in R to produce your
final doc.

To leave a comment for the author, please follow the link and comment on his blog: socialdatablog » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.