-
Notifications
You must be signed in to change notification settings - Fork 22
literate programming (documents with code): knitr, R Markdown, Sweave, Latex, etc.
Christopher Paciorek edited this page Sep 14, 2013
·
34 revisions
- Formatting long lines: http://tex.stackexchange.com/questions/41471/getting-sweave-code-chunks-to-stay-inside-page-margins
- To split long lines of bash code within an Rnw or Rtex file try this:
echo "my long line\
of stuff" > tmp.txt
- or this to split outside of a string. (Make sure there is no space after the "")
echo "my long line"\
> tmp2.txt
Here are some thoughts.
-
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.
-
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.
- 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.
- 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).
- 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.
-
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.
- 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.