Weaving Rnw Files in the RStudio IDE


Weaving Rnw Files


Rnw (or Sweave) files support authoring documents that contain a mix of content and R code, allowing you to execute and embed the results of R computations and graphics within a document. This enables the creation of dynamic reports that are updated automatically if data or analysis changes.

The process of "weaving" an Rnw file involves executing R code and inserting it into the document. The traditional method of weaving Rnw files is the version of Sweave that is included within the base distribution of R. In addition to Sweave, RStudio also supports using the knitr package to weave Rnw files.

You can customize the method used to weave Rnw files on a global, per-project, and per-file basis.

Setting Global Defaults

You can set the global default method for weaving Rnw files from within the Authoring section of the Global Options dialog. Setting this option has the following effects:

  1. It determines the method used for weaving Rnw files when you are not within a project.
  2. It is used to initialize the method for weaving Rnw files for newly created projects.

Note that changing the global default will not change the default for previously created projects. The section below includes information on setting per-project defaults.

Setting Project Defaults

RStudio projects have an option to indicate which method of weaving Rnw files should be used when they are active. This option is initialized based on the global default and can be changed using the Sweave section of the Project Options dialog:

If you have a project containing Rnw files that need to be processed using more than one method the following section describes how to set the Rnw method on a per-file basis

Setting the Weave Method for a File

You can also specify the method to be used for Rnw weaving on per-file basis. You may want to do this either to override the global or project-level default, or if you want to share the file and ensure that the correct method of weaving is used on other systems.

To specify Rnw weaving for a file, you add a specially formatted TeX comment. For example, to specify that you want the current file processing using knitr, you would use the following code

% !Rnw weave = knitr

To specify Sweave you would use:

% !Rnw weave = Sweave

When opening a file with an embedded Rnw weave comment, RStudio checks to ensure that the package required for the specified weaving method is available. If the package isn't available then the user is notified that they'll need to install it in order to successfully compile a PDF from that file.

Additional Rnw Weave Options

The following additional Rnw options can also be configured from within the Authoring section of the Options dialog:

  • Always enable Rnw concordance — Automatically set the options required to emit concordance information during Rnw weaving. Concordance provides a mapping between line numbers in the Rnw file and the generated TeX file. This enables both direct navigation to compilation errors within Rnw files and compatibility with Synctex for PDF previewing.

Related Topics