Project Reporting with RMarkdown


RMarkdown  provides an authoring system for project and data science reporting.  RMarkdown is a core component of the RStudio IDE.  It braids together narrative text with embedded chunks of R code.  The  R code serves to demonstrate the model concepts in the text.  RMarkdown  produces elegantly formatted document output, including  publication quality data plots and tables.

RMarkdown integrates several applications into an easy to use framework.  The core components include the R programming language and Markdown, which is a light weight markup language for text creation.  RMarkdown then relies on the knitr package to call the R interpreter and to produce model results, report tables and charts.  The pandoc application is called next and serves as a document conversion tool that renders text in different output formats.  pandoc also has the ability to render {\footnotesize \LaTeX} commands for state-of-the-art typesetting, equation editing and document control.

Benefits of RMarkdown

The benefits of combining text authoring with scientific programming are significant:

  • Reported results are immediately reproducible given embedded code and data objects;
  • Code will often detail incremental calculations that the academic text might overlook given the need to simplify text content and equations
  • Reports can be easily updated as data or code objects are updated … no  copy/paste effort across files or applications is required;
  • Collaboration and communication is greatly enhanced within teams and across teams;
  • Dozens of output formats are supported, catering to a wide variety of authoring needs.

Perhaps most important, the RStudio IDE delivers integrated code and text authoring so the entire process is easy to implement and use.


The following applications need to be in place, regardless of the operating system used:

  • R programming language
  • RStudio
  • Tex (markup and typesetting language for high quality {\footnotesize \LaTeX})
  • Compilers for other programming languages (optional)

The RMarkdown packages also need to be installed, which install dependent packages like knitr, among others:

How It Works

The following sequence defines the standard RMarkdown work flow:

  1. Open a new .Rmd file in RStudio with File ▶ New File ▶ 
RMarkdown. Use the wizard that opens (shown at left) to pre-populate the file with a template options;
  2. Write document by editing template with text narration and code chunks (examples below);
  3. Knit document to create report by using point-and-click knit button to render the text document or use the render() command in console to knit;
  4. Preview document output in the IDE window;
  5. Publish file output (optional) to a web server;
  6. Examine build log in RMarkdown console in case of errors;
  7. Use output files (such as *.tex, *.pdf, *.doc, etc) that are created  by pandoc and saved with the original*.Rmd file.

RMarkdown Example

The plain text below is an RMarkdown file with the extension *.Rmd:

The plain text file contains three important types of content:

  1. A header section surrounded by with core document controls;
  2. Blocks of R code surrounded by `; and
  3. Text narration mixed with simple text formatting like ##sub-heading and  **bold**.

The example above is very simple in nature with only minimal formatting of text, data and plots.  The rendered output looks like this:

Document control and quality are explored next.

Text Formatting

Some basic text formatting commands appear below:

[table id=85 /]

Top quality document formatting can also be achieved by embedding {\footnotesize \LaTeX} markdown commands into the Rmarkdown file.  An  intro to {\footnotesize \LaTeX} and many common commands can be found here

Global Document Options

Document output formats are controlled by option controls in the RMarkdown header. The following example shows an expanded header section.  The output section is now set to produce a PDF file and an HTML file for web-site use. Hence. multiple output documents can produced simultaneously:

Looking closer at the header details above, several observations are worth sharing:

  • The output options will save the {\footnotesize \LaTeX} *.tex file created by pandoc to render the PDF file. This can be useful for debugging large markdown documents (either in RStudio or in Tex, where more detailed debugging options are available);
  • The PDF output has been configured to include section and figure numbering, which is typical of more formal documents.
  • The header-includes section now attaches a large number {\footnotesize \LaTeX} packages for high quality document control. Included packages enable equation editing, figure inserting and layout placement, non-standard font colors, footnoting, improved hyperlinks, and the ability to switch page layouts from portrait to landscape within a single document.
  • Finally, the header defines a sub-title, page geometry or dimensions and specifies a European paper size.

For large documents, the header section expands during document development as new options are required. As a result, some headers can be substantial.

Document Format Types

The following output formats are available to use with RMarkdown.  The supplied links provide detailed format instructions for setting up the header of the RMarkdown file:


Presentations (slides)


You can also build books ⧉, websites ⧉, and interactive documents ⧉ with RMarkdown. There are also package solutions that utilize RMarkdown to format text to the specs of different academic journals.

Code Chunk Options

The next section defines options to control the behavior of code chucks in the RMarkdown file:

[table id=87 /]

Use of Other Programming Languages

It is important to acknowledge that code chunks from other languages can also be integrated into RMarkdown documents. The support for multiple languages comes from the knitr package, which has a large number of language engines.  For example, there are 50 programming languages supported by knitr:

To use a different language engine, simply change the language name in the chunk header from R to the engine name:

Shell scripts like bash (for the Linux and OSX operating systems) can also be run in RMarkdown as follows:

Back | Next