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

[This article was first published on mpastell.com » R, 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.

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.

To leave a comment for the author, please follow the link and comment on their blog: mpastell.com » R.

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)