R includes a powerful and flexible system (Sweave) for creating dynamic reports and reproducible research using LaTeX. Sweave enables the embedding of R code within LaTeX documents to generate a PDF file that includes narrative and analysis, graphics, code, and the results of computations.
knitr is an R package that adds many new capabilities to Sweave and is also fully supported by RStudio.
To use Sweave and knitr to create PDF reports, you will need to have LaTeX installed on your system. LaTeX can be installed following the directions on the LaTeX project page.
Working with Sweave and Knitr
Creating a New Document
To start a new Sweave document, go to File | New and select “R Sweave”. This will provide a basic Sweave template. From here, you can enter text and LaTeX commands. R chunks can also be inserted to interweave R commands and output into your document. To insert an R chunk, use the Chunks menu at the top right of the source editor.
You can compile the Sweave document into a PDF using the Compile PDF button on the toolbar. Note that for reproducibility purposes, this compile runs in a separate process and environment (rather than using the current workspace). This is to ensure the script will always produce the same result and not be effected by (or pollute) your regular R environment.
You can use knitr rather than classic Sweave for weaving Rnw files. See the Weaving Rnw Files documentation for additional details on how to enable the use of knitr on a global, per-project, or per-file basis.
PDF Preview and SyncTeX
RStudio supports previewing PDFs using SyncTeX, which enables high-fidelity synchronization between PDFs and the source files that generated them. Using SyncTex you can:
- Click on a line of code within a source file to navigate the PDF viewer to the corresponding location in the PDF.
- Click on a location within a PDF file to open the source editor to the corresponding line of source code.
RStudio includes a built-in PDF viewer that natively supports SyncTeX. On some platforms RStudio also integrates with a native PDF viewer that supports SyncTeX (the default PDF previewing configuration on each platform is described below). For additional information on SyncTeX, see the documentation on PDF Preview.
Customizing LaTeX Options
The RStudio Compile PDF command typesets the current TeX or Rnw source file into a PDF. There are a number of options you can specify to control this process, the most important of which is which LaTeX program you want to use for typesetting. See theCustomizing LaTeX Options documentation for additional details.
Spell checking is available for Rnw files. To check your document, click the spell checking icon on the source editor toolbar. Alternatively you can select “Check Spelling…” off the Edit menu. The shortcut key is F7. For additional information on language dictionaries and spell checking, see the Spelling Dictionaries documentation.
Working with Multiple Rnw Files
In some cases you may wish to break a larger Sweave document into multiple Rnw input files. To support this RStudio allows for the specification of a root Rnw file that is always used as the target for PDF compilation. The root file can be specified on a per-file or per-project basis. RStudio also uses the concordance file generated by Sweave to ensure that both Synctex and TeX error log navigation work correctly in the presence of multiple Rnw files. For more information, see the Working with Multiple Rnw Files documentation.