Create Dynamic R Statistical Reports Using R Markdown

Printer-friendly versionPrinter-friendly version

Introduction - What is R markdown?

According to RStudio,

"R Markdown is a format that enables easy authoring of reproducible web reports from R. It combines the core syntax of Markdown (an easy-to-write plain text format for web content) with embedded R code chunks that are run so their output can be included in the final document".

In this document, we will introduce how to create dynamic R statistical reports in HTML.

  • What do you mean by dynamic? The traditional way to write a report is save your analysis from R first, i.e. saving the ANOVA table or using pdf() to save the graphs. A report written in this way could be problematic.  For instance, imagine your client telling you that they want to use a sub-sample instead of the entire sample. You would have to redo all of your work!! Therefore, in this way dynamic also means reproducible, in the sense that people who get the file from you can reproduce the entire work in the report.
  • How does R Markdown work out to be .html file? R Markdown is the file with the file extension .Rmd, the knitr package will then transform the file into a Markdown file with the extension .md. Then Rstudio will load another package called markdown to transform the file into .html.
  • Is this a popular method for creating reports? Check out Rpubs. This website shares lots of documents written in the way we will introduce below.

Let's take this in four steps:

  1. Getting the Software Ready
  2. Creating a Simple Report
  3. Customizing your CSS  to Make Your Report Pretty
  4. Become a Ninja!

Step 1: Getting the Software Ready

  • Download R studio at : http://rstudio.org/
  • Set options for R studio: Tools > Options > Click on Sweave and choose Knitr at “Weave Rnw files using Knitr”. Check Image

Step 2:  Install the Knitr Package

  • To install a package in RStudio, you use Tools > Install Packages and then select a CRAN mirror and package to install. Another way to install packages is to use the function install.packages(). For example:

# installs the knitr package from the Carnegi Mellon Statlib CRAN mirror
install.packages("knitr", repos = "http://lib.stat.cmu.edu/R/CRAN/")

Step 3: Creating a Simple Report

  1. Create a blank R Markdown file: File > New > R Markdown. Check Image here You will open a new .Rmd file.

  2. When you create the blank file, you can see an already-written module. One simple way to go is to replace the corresponding parts with your own information.

  3. If you want to write a completely new file, check the markdown reference. It's only one page and you can find this on the right-hand panel of Rstudio by clicking MD.

    MD

  4. After all your information is entered, click Knit HTML.

    Knit HTML

Now you will see that you have generated an .html file. What happens after you click Knit HTML? Simply speaking, the knitr package is loaded and the function knit() is applied to get the markdown file you wrote. Then RStudio uses markdown package to convert markdown to HTML.

Step 4: Customizing your CSS  to Make Your Report Pretty

Here we use table as an example. The R Markdown syntax for a table is:

 First Header  | Second Header
 ------------- | -------------
 Content Cell  | Content Cell
 Content Cell  | Content Cell

In your output file, you will have a table that will then look like this:

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

Not very pretty, it is! What we can do now is customize the CSS. We will set the CSS for table by applying a bit or CSS code such as:

/*  CSS chunck 1  */
th {  background-color:#E0E0E0 ;
      border-bottom:1px solid black;
      padding:5px;}

td{
border-bottom:1px dotted black;
padding:5px;}

table{ 
border-collapse:collapse;
margin:auto;
 border: 1px solid black;}

To embed the CSS file, there are two methods:

  1. At the beginning of the .Rmd file, place the following code:

    <style type="text/css">
    
    Insert CSS chunk 1
    </style>
    
  2. Put the CSS chunk 1 in a .css stylesheet and then at the beginning of the file, add the following code:

    <link href="custom.css" rel="stylesheet">
    

By customizing the CSS file, you will have created a brand new-look for the table:

table

Step 5. Become a Ninja!

There is much more for you to play with, please check the website: http://yihui.name/knitr/ for more chunk options and package options.

Want to Learn More? 

Register for STAT 497R: Topics in R: Statistical Language (1 credit)

Page Author - Yuan Huang