Looping through a set of graphics in odfWeave

[This article was first published on Social data blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

Permalink | Leave a comment  »

To leave a comment for the author, please follow the link and comment on their blog: Social data blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)