Tutorial: Sweave - Writing dynamic and reproducible documents

Friedrich Leisch, Department of Statistics, Ludwig-Maximilians-Universität Munich, Germany


Sweave is a tool that allows the R code used for a complete data analysis to be embedded into LaTeX, HTML or OpenOffice documents. The purpose is to create dynamic reports, which can be updated automatically if the data or analysis change. Instead of inserting a prefabricated graph or table into the report, the master document contains the R code necessary to obtain it. When run through R, all data analysis output (tables, graphs, etc.) is created on the fly and inserted into a final document. Data, code and documentation are tightly linked together, which allows for truly reproducible research.


This tutorial will first give an overview of various Sweave file formats, and then concentrate on the most popular version, embedding R code into LaTeX documents. We will discuss and demonstrate the various formatting options that can be used and how they affect the final layout. Practical examples will show how to Another important aspect is which parts should be programmed in the R code chunks, and when it is more efficient to use TeX as a programming language. We will finish with exciting new Sweave features for R 3.0.

Intended Audience

UseRs who are interested in learning how to write dynamic documents with R or who want to improve their skills to exploit the full power of Sweave.

Required Knowledge

Familiarity with LaTeX or other markup languages like HTML is a plus, but not strictly necessary (the basic structure of a LaTeX document and the concept behind it can and will be explained in 5-10 minutes). Basic knowledge of using R for data analysis is required.