knitr::opts_chunk$set(echo = TRUE)

Introduction to R Markdown

This lab will show you how to write an R Markdown document.

General Goals

In this lab, you will learn:

  1. What R Markdown is
  2. How to create an R Markdown file
  3. How to use the most common R Markdown syntax
  4. How to add code in your R Markdown file
  5. How to format your text and equations in R Markdown

Lab templates

You will be completing each lab and writing assignment (WA) by writing your code and notes in the corresponding R Markdown document.

For example, for lab 1, open the answer template Rmd file called Lab_1.rmd in your project’s directory for lab 1. Use it to add your code and notes for the lab. For WA 1, open the answer template Rmd file called WA_1.rmd in the same folder in your directory.

Markdown

Markdown is a way of adding to a plain text document formatting, such as headers, lists, emphasis, quotes, links to URLs and images, and more.

Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to HTML.

R Markdown

R Markdown allows you to write HTML, PDF, and/or MS Word documents, a presentation, or even a book by just using Markdown syntax and embedded programming code.

R Markdown files have extension .Rmd

The “R” in R Markdown stands for the R statistical computing language.

R Markdown files contain three important types of content:

  1. An (optional) YAML header surrounded by —s.
  2. Chunks of R code surrounded by ```.
  3. Text mixed with simple text formatting like # heading and italics.

We will cover these contents in this lab.

Creating Rmd Files (Cocalc.com)

To create an Rmd file in Cocalc. com, go to the directory (location) in your project where you want the file to be. Then:

  1. Click on “New”
  2. Choose the option “RMarkdown (.rmd)”
  3. The system will prompt you to enter a name for the file (optional)
  4. Click “Create”

Figure: Creating Rmd files

Example

Create an Rmd file named “My_File” in the same directory where this lab is located.

Load Packages

Packages are collections of R functions, data, and some compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others packages are available for download and must be installed.

In Cocalc, many packages are already installed, so there is no need to install them.

Once a package is installed, they must be loaded into the session to be used.

To load a package in R, we invoke the command library(package) where in ‘’package’’ will be replaced with the name of the package you want to load.

Here is an example on how to invoke a package in R Markdown.

library("knitr")

Note: To run any R chunk of code, you must use the 3 backticks followed by R in curly braces and after the code you want to run, close the chunk with another set of 3 backticks. More on this later in this lab.

The knitr package extends the basic markdown syntax to include chunks of executable R code.

Cocalc already have this package loaded for you. But if you are running your code, say in R Studio on your own computer, you would need to install and include the knitr in the library of your document or script.

Example

Now load the package called dplyr.

Enter your code below:

Note: You will learn how to install packages for R in Cocalc in lab 2 (Stay tuned!!!).

Formatting text

I hope you already noticed some of the formatted text above in this rmd file.

Here some basics formatting of text in Markdown.

Comments

Note that you can add comments in Markdown documents as follows: <!-- your comment -->. The comments will not be displayed in any output format.

Compare this line here in the rmd file (on the left in Cocalc.com) and the output format in HTML on the right (Note that the comment below is missing!!.

Plain Code Blocks

Plain code blocks are displayed in a fixed-width font but not evaluated by R.

2*3
This text is displayed verbatim

You will see this throughout this rmd file to display examples of codes that are not evaluated.

Headers

You can include section headers by using pound signs.

# First-level header

## Second-level header

### Third-level header

Emphasis

*italic*   **bold**

_italic_   __bold__

Note that the word Statistics is in italic here and the work markdown is in bold.

Here is an example: Bianca Sosnovski

Example

Write your full name in both italic and bold font.

Enter your answer below:

Lists

Unordered list
  • Item 1
  • Item 2
    • Item 2a
    • Item 2b

The code for the list above is:

* Item 1
* Item 2
    + Item 2a
    + Item 2b
    
Ordered list
  1. Item 1
  2. Item 2
  3. Item 3
    • Item 3a
    • Item 3b

The code for the list above is:

1. Item 1
2. Item 2
3. Item 3
    + Item 3a
    + Item 3b

Example

Create a list of textbooks that you will need to buy and/or download for free for your classes this semester.

Enter your code below:

Embedding R code

As you see at the beginning of this lab, you can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
#cars is a built-in data set in R

Note that the line preceded by # is a comment in the R code and that you can add comments in the R code chunk with a pound sign.

Example

Enter a R code chunk that produces as a result the number 6.

Enter your code below:

To add an inline code, use a pair of single backticks and the letter r. For example, 6 (compare this with the Rmd file). We can also mark text as inline code by using just the pair of backticks. For example, code.

You can also embed plots in your rmd files. For example:

You can do a lot of things in a code chunk: produce text output, tables, or graphics. You have complete control over all these output via chunk options, which can be provided inside the curly braces (between {r and }).

Note: the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot (if applicable).

Example

Create a code chunk that will produce the summary of the dataset Seatbelts.

Enter your code below:

Example

Create a code chunk that will produce a plot of the dataset Seatbelts.

Enter your code below:

Writing mathematical equations and formulas

Of course, you can include math formulas by using dollar signs around the equation in Latex syntax (Using $ symbols around the equation).

The following is the code for writing the equation of the area of a circle in two formats.

In line

$ A= \pi r^2 $

In equation display

$$ A= \pi r^2 $$ 

Example

Write the equation of the circumference of a circle.

Enter your code below:

References

Parts of the material used in this document are adaptations or excerpts from the following: