R Markdown

Overview

R Scripts are excellent tools for conducting data analyses, but they are not always the best format for sharing your work with others. An R Script records the steps of an analysis, while an R Markdown document communicates the entire research process by combining code, results, and explanations into one reproducible report.

The purpose of converting an R Script into R Markdown is not simply to run code. It is to create a complete, readable report where another person can understand your analysis from beginning to end.

R Markdown

An R Script contains the instructions needed to perform an analysis; however, someone else must have access to your script, your data files, and R software to reproduce your results.

An R Script shows how an analysis was performed, but it does not automatically include the results, graphs, tables, or explanations needed to understand the findings. R Markdown solves this problem by combining multiple types of information into a single document.

R Script vs. R Markdown

Feature R Script (.R) R Markdown (.Rmd)
Main Purpose Write and execute R code. Create a complete report combining code, results, and explanations.
Contains Primarily R code and comments. Code, text, tables, graphs, and statistical interpretation.
Output Results appear in the Console or Plot pane. Creates a formatted report such as an HTML document.
Sharing Requires others to have R, your files, and your script. Can be shared through a web link without requiring R or RStudio.

How to Create an R Markdown Report

The following steps describe the process for converting an R analysis into a complete R Markdown report. In this course, you will use R Markdown to organize your statistical analyses and create shareable HTML reports.

Step 1: Install the R Markdown Package

Before creating an R Markdown document, you must install and load the rmarkdown package.

install.packages("rmarkdown")

library(rmarkdown)

Step 2: Create a New R Markdown File

In RStudio, create a new R Markdown document:

File → New File → R Markdown

A window will appear asking for information about your document.

Setting What to Enter
Title Enter your assignment or project title.
Example: Assignment 1
Author Enter your name.
Output Format Select HTML.

Click OK. A new R Markdown file will open.

RStudio automatically creates a sample R Markdown document. Delete the example text and code before beginning your assignment.

Step 3: Copy Code From Your R Script

R Markdown documents contain two types of content:

Content Type Purpose
Narrative Text Written explanations that describe your methods, results, and interpretations.
Code Chunks R code that performs calculations, creates graphs, and produces statistical output.

Copy the analysis code from your existing .R script file and paste it into your new .Rmd file.

Step 4: Convert Your R Script Into R Markdown Format

An R Script and an R Markdown document use different formatting rules.

When transferring code from an R Script into R Markdown, you must separate your explanations from your R code.

Outside Code Chunks: Written explanations, instructions, and interpretations.

Inside Code Chunks: R code that will be executed by the program.

Converting Comments Into Written Text

In an R Script, comments are created using the # symbol. Comments are notes for yourself and are ignored by R when the code runs.

In R Markdown, explanations should be written as regular text instead of comments.

R Script R Markdown
# Calculate descriptive statistics
Calculate descriptive statistics.

Creating Code Chunks

R code must be placed inside a code chunk so that R Markdown knows which instructions should be executed.

Code chunks begin with three backticks followed by {r} and end with three backticks.

```{r}

2 * 2
  
```