Skip to content

literate programming (documents with code): knitr, R Markdown, Sweave, Latex, etc.

Christopher Paciorek edited this page Sep 15, 2013 · 34 revisions

Some miscellaneous tips

Matt's contribution on breaking lines

So you put this as the first chunk in your Rnw document. It adjusts the preferences for all of the following chunks of code such that knitr runs the code and then passes both the source code and the output to the listings package for formatting.

    <<setup, include=FALSE, cache=FALSE>>=
    library(knitr)
    opts_chunk$set(fig.path = 'figure/listings-')
    options(replace.assign = TRUE)
    render_listings()
    @

This defaults to a kind of strange color scheme for the actual PDF output. You can change the fonts and the colors by adjusting the listings package preferences in the Sweavel.sty file that is created after you knit the document for the first time. This link includes a list of some of the settings that you can change: http://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Using_the_listings_package

Note that if you look at the Sweavel.sty file, there is by default a "breaklines=true" setting that is actually causing the line breaking.

Tessa's overview of document creation

Here are some thoughts.

  1. Latex is a coding/typesetting language that takes a source (text) file, in which the user has typed raw text as well as making use of functions to denote complicated structures like equations, greek letters, mathematical symbology, tables, and figures. This source file is saved as a .tex file, e.g. 'filename.tex'. Executing 'pdflatex filename.tex' at the command line outputs a pdf file containing the nicely formatted text and beautifully rendered mathematical symbology. This typesetting step can also be accomplished using a gui like TexShop or TexnicCenter, which will take any .tex file and turn it into a .pdf file.
    
  2. R is a computing language that takes a source (text) file, in which the user has typed raw text, making use of functions to create objects which may be displayed as numerical values, plots, or other kinds of output, which a user may wish to include in a nice report, as generated by Latex.
    
  3. Each of the languages above have their own syntax and their own file structures. Traditionally, including output from R in Latex is done by copying and pasting, which is inefficient and error prone.
  4. Sweave and knitr are a way of weaving or knitting together the two types of sources in order to produce a unified output. Sweave (and its .Rnw extension) predates knitr (and its .Rtex extension).
  5. Making use of these begins by creating a source (text) file, with either the .Rnw or .Rtex extension. The file contains raw text, some of which refers to things that should simply be typeset and formatted (such as mathematical equations, or written commentary about what is being done), and some of which should be evaluated and placed into the formatted document (such as code snippets, numerical values, or plots). The text file must somehow indicate which items are to be simply typeset and formatted (as would appear in a regular .tex file), and which bits need to be executed before being typeset (as in a .R file). This syntax is what differs between .Rnw and .Rtex. I would suggest you look at the knitr demo page for examples.
  6. As the homework assignment says, knitting the .Rnw or .Rtex file in R will output a Latex file. This is a standard .tex file, as I explained it in (1). You might try opening that .tex file to see what it looks like. Look for similarities and differences between your original .Rnw/.Rtex and the new .tex file. 
    
  7. This new .tex file is still simply a file full of text, waiting to be turned into a .pdf document with nice symbols, plots, etc. As explained in (1), the .tex file must be compiled/typeset into a .pdf, in one of the manners explained there.