# Create odf, pdf and html report from a single Sweave document

March 24, 2010
By

A lot of us know about Sweave and Latex and they work very well in creating elegant dynamic reports from R computation. However, sometimes we would like to also produce a word processing document for a colleague or a html version of the same report. Now there are tools for producing these like odfWeave. But wouldn't it be nice to get all of the output formats from the source document with same mark up?

Today I'm going to write about a good option how to get a nice looking odf, pdf and html report from the same source: ascii package with reStructuredText (reST) markup. reStructuredText is a simple plain text mark up that can be converted to several formats using the Python Docutils. It is simpler to write than Latex and it also the syntax that is used in Sphinx documents. The ascii package has a Sweave driver for weaving R code from documents written with reST and some other mark up languages as well. I have created a simple example document ascii-example.Rnw, which demonstrates the capabilities of the combination. The source file is a Sweave document with reST mark up instead of Latex. I had to make small modifications to the in ReST driver in ascii package to get this example to work1 (download it here: newRest.R). I then processed the example with the new driver in R:

> library(ascii)
> source('newRest.R')
> ReST('ascii-example.Rnw')


Weaving produced this ascii-example.rst reST document which I then converted to odf, html, pdf with pdflatex and pdf with rst2pdf using:

rst2odt ascii-example.rst ascii-example.odt
rst2html ascii-example.rst ascii-example.html
rst2latex ascii-example.rst ascii-example.tex && pdflatex ascii-example.tex
rst2pdf ascii-example.rst


And here are the results: odt, html, pdf from latex and pdf from rst2pdf. You'll need to have Docutils and odtwriter for the conversion.

I have used the default options, but there are a lot options that can be passed to the docutils writers to customize the output, such as custom stylesheets. The image format in the pdfs is not optimal, but in can be changed with the "res" option in code chunk or you can also choose to use pdf format instead of jpg, but then it won't show up in html documents. I think using reST with ascii package is a good option for producing reports and tutorials in multiple output formats. That is also because I'm already familiar with reST directives and also use Sphinx for other purposes too. The reST syntax is also very fast to learn and I think it is definitely worth exploring. I also recommend using Sphinx if you only want to get html and pdf output, because it has more directives than plain reST e.g. math support.

1. I changed the driver to output code chunks in reST literal environment :: instead of .. codeblock:: r, because the codeblock directive is not supported in odt conversion and very poorly supported in latex conversion. Ôå®