Working with Multiple Rnw Files in the RStudio IDE


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, the RStudio IDE 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.

NOTE: If you are using knitr rather than Sweave be sure to review the Additional Considerations for knitr section below.

Specifying a Root Document

For example, you might have a root Rnw (Master.Rnw) that includes a child file as follows:


This is the overview.



The Rnw file included from within the root (Child.Rnw) would contain a special directive associating it with Master.Rnw:

% !Rnw root = Master.Rnw

This is the analysis.


The Rnw root directive at the top of the child file tells RStudio that when you execute the Compile PDF command while editing the child that Master.Rnw should be the target for compilation.

Note that some TeX editors including TeXworks and TeXShop also support this type of directive. For documents which you expect to edit using both RStudio and one of these editors the TeX variation of this directive is also supported:

% !TeX root = Master.Rnw   

The path specified in the root directive is relative to the file it is contained in and can reference the parent directory using ".." (for example: root = ../Master.Rnw).

Setting a Project Level Root Document

In some cases it might be more convenient to set a project-level root document so that all PDF compilation within a project automatically targets the root file. You can do this using the Sweave project options panel:

Note that the file level directives described in the prior section take priority over any project level root file you have specified.

Additional Considerations for knitr

If you are using knitr for multi-file compilations you should ensure that you are using knitr version 0.6 or later, as that version included important enhancements to generating concordance for multi-file compilations.

In addition, you need to add the following option to your Rnw file in order for Rnw file/line error reporting and PDF to source syncing to work correctly with multiple-file compilation:


You can also specify this option in your .Rprofile if you don't wish to include it in every Rnw file.

For knitr, it's also preferable to include child documents using the child chunk option rather than \SweaveInput. For example:

<<test, child='Child.Rnw'>>=

Finally, there are some known limitations in the accuracy of file/line concordance in multiple-file knitr documents. In these cases the file/line reported for LaTeX compilation errors or result of a PDF sync may be slightly inaccurate.


Related Topics