Introduction

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.

Let’s watch a short video introducing R Markdown.


Getting Started with R Markdown

The Title, Author and Output format choices will be used to create the YAML header and are used to customize your document. By default, the date is also included. A subtitle can be also be added.

Additional information that can be added to customize your HTML document include:

When you click the Knit button a document will be generated that includes both non-code content (text, links, images, plots, tables, etc.) as well as the output of any embedded R code chunks or in-line text within the document.


The Basics

Reports created using Markdown include a combination of text and code output.

Headings

Section headings are creating using #, followed by the heading text. The largest heading uses a single #. Additional # can be included to denote lower level headings.

First Level Heading: # Heading

Second Level Heading: ## Heading

Third Level Heading: ### Heading

Fourth Level Heading: #### Heading

Text

  • italics : *text*
  • bold: **text**
  • both: ***text***
  • Strikethrough : ~~text~~
  • Superscriptt : text^t^
  • Subscriptt : text~t~

Lists

Unordered lists

Unordered lists can be created using - or *. Lower level bullets can be created using + and indenting.

  • Bullet 1
    • Bullet 1a
    • Bullet 1b
  • Bullet 2
    • Bullet 2a
  • Bullet 3
    • Bullet 3a
    • Bullet 3b

Ordered Lists

Ordered lists can be created using numbers followed by a period (.). Lower level bullets can be created using + and indenting.

  1. Bullet 1
    • Bullet 1a
    • Bullet 1b
  2. Bullet 2
    • Bullet 2a
  3. Bullet 3
    • Bullet 3a
    • Bullet 3b

Activity #1

  1. Create a new R Markdown file. Choose HTML as the output type and name the document “In Class Activity”.

  2. When a new markdown file is created, it includes example code. Knit the document and save the file to your computer as “ICA.Rmd”.

  3. Remove the example code (lines 12-30). Add a Level 2 heading that says “My Activity Solutions”. Change the theme (not cerulean) in the YAML header.


Including Code in R Markdown

Code Chunks

To incorporate the output of running code into your report, you need to use R code chunks, which are code lines between a set of triple back ticks. To add a code chunk, you can use the icon and choose R.

Below, we run the summary() function on the built-in cars dataframe.

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

Customizing Code Chunks

As you see, the default prefix of the result of running a line of code is ‘##’. We can remove this by setting comment = "".

dim(cars)
[1] 50  2

If you want to display the R code, but not run the code (or display code output), you would use eval = FALSE:

dim(cars)

If you do NOT want to display the R code, only the output of running the code, you would use echo = FALSE:

## [1] 50  2

In-Text Code

To include code in-text, you can use a set of back ticks. For instance: To obtain the minimum of the first variable in the cars dataframe, we would use min(cars[,1]). To incorporate the results of running code in text, we include r after the first back tick. As an example, the mean of the speed variable in the cars dataset is 15.4 and the variance is 27.96. The minimum value of the dist variable in the cars dataset is 2 and the maximum value is 120.


Plots & Figures

Plots

Plots created using R can be included in a Markdown document by generating the plot in a code chunk. If echo = FALSE, the plot will display but not the code used in the code chunk to create the plot.
Figure 1

Figure 1

Options to customize the plot appearance include:

  • fig.align: “left”, “right” or “center”
  • fig.cap: provide a figure caption
  • fig.width: specify figure width (inches, default 7)
  • fig.height: specify figure height (inches, default 5)

Images

To include images that are located in the same folder as your .Rmd file, you can reference the image file by name using ![caption]('filename.fileextension').

LeBow College of Business

LeBow College of Business

Note: In this example, I use a link to a shared image from google drive.


Activity #2

  1. Using the built in dataset named attitude, use a code chunk to obtain summary information. Do not include the code in your document. Only include the output.

  2. Using in-line code, complete the following sentence by filling in the missing information (rounded to 2 decimal places):

The average rating value is _________, with a variance of _________ and a standard deviation of _________. The minimum value of the complaints variable is ________ and the maximum value is ________.

  1. Create a scatterplot with complaints on the x-axis and rating on the y-axis. Give the plot a meaningful title, axis labels, and report caption.

Miscellaneous

Tables

Default tables can be created using the print() function on a dataframe or matrix object or running a code chunk of the dataframe or matrix name. To avoid printing ## in our results, we set comment = "".

head(cars)
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10

Tables can be included using the kable() function in the knitr package. These tables can be dataframes or the output from functions such as table() and aggregate(). To omit the code used to create the table, use echo = FALSE.

Note: Before using, you may need to install the package.

knitr::kable(x = head(cars), 
             caption = "Cars Dataset Preview")
Cars Dataset Preview
speed dist
4 2
4 10
7 4
7 22
8 16
9 10

Function arguments that can be used to customize the table include:

  • caption: A title for the table
  • digits: the maximum number of digits to include in the table output
  • row.names: logical (TRUE/FALSE) identifying if row names should be included
  • col.names: a character vector of column names (if not provided, defaults to column names in dataframe)
  • align: “l” (left, default for non-numeric variables), “r” (right, default for numeric variables), “c” (center)

References

In-text references can be used in the form of footnotes using [^n] where you want the footnote to appear (n is the number of the footnote).

Here is a sentence with a reference to a footnote1


Activity #3

  1. Create a table that displays the first 5 observations in the complaints, raises and rating columns in the attitude dataframe. Include “Table 1: attitude” as the table caption, align the table center and do not include the code, only the output.


  1. Here is the text to appear in the footnote.