1 R Markdown Basics

Here is a brief introduction into using R Markdown. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. R Markdown provides the flexibility of Markdown with the implementation of R input and output. For more details on using R Markdown see https://bookdown.org/yihui/rmarkdown/.

Be careful with your spacing in Markdown documents. While whitespace largely is ignored, it does at times give Markdown signals as to how to proceed. As a habit, try to keep everything left aligned whenever possible, especially as you type a new paragraph. In other words, there is no need to indent basic text in the Rmd document (in fact, it might cause your text to do funny things if you do).

1.1 Lists

It’s easy to create a list. It can be unordered like

  • Item 1
  • Item 2

or it can be ordered like

  1. Item 1
  2. Item 2

Notice that I intentionally mislabeled Item 2 as number 4. Markdown automatically figures this out! You can put any numbers in the list and it will create the list. Check it out below.

To create a sublist, just indent the values a bit (at least four spaces or a tab). (Here’s one case where indentation is key!)

  1. Item 1
  2. Item 2
  3. Item 3
    • Item 3a
    • Item 3b

1.2 Line breaks

Make sure to add white space between lines if you’d like to start a new paragraph. Look at what happens below in the outputted document if you don’t:

Here is the first sentence. Here is another sentence. Here is the last sentence to end the paragraph. This should be a new paragraph.

Now for the correct way:

Here is the first sentence. Here is another sentence. Here is the last sentence to end the paragraph.

This should be a new paragraph.

1.3 Font syntax

italics or italics

*italics* or _italics_
**bold** or __bold__

superscript2

superscript^2^

subscript2

superscript~2~

strikethrough

~~strikethrough~~

1.4 R chunks

When you click the Knit button above a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this (cars is a built-in R dataset):

The basic R chunk is:

```{r cars}
summary(cars)
```

The result is:

##      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

1.4.1 Chunk options

You can define chunk options globally or individually. A global definition can be inserted into your file by calling the knitr::opts_chunk$set function. A global definition can be modified by individual definitions.

1.4.1.1 Global options

```{r}
knitr::opts_chunk$set(options)
```

1.4.1.2 Individual options

```{r, options}
R code
```

1.4.1.3 Some chunk options

  • Text output

    echo:(TRUE or FALSE) Whether to display the source code in the output document.

    collapse: (TRUE or FALSE) Collapse all the source and output blocks created by the chunk into a single block.

    warning: (TRUE or FALSE) Whether to preserve warnings (produced by warning()) in the output.

    error: (TRUE or FALSE) Whether to preserve errors (from stop()).

    eval: (TRUE or FALSE) Defines whether R code should be run or not.

For a detailed list of options, see:

[link] https://yihui.org/knitr/options/

[link] https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf

1.5 Inline code

If you’d like to put the results of your analysis directly into your discussion, add inline code like this:

> The `cos` of $2 \pi$ is `r cos(2*pi)`. 

The cos of \(2 \pi\) is 1.

Another example would be the direct calculation of the standard deviation:

> The standard deviation of `speed` in `cars` is `r sd(cars$speed)`.

The standard deviation of speed in cars is 5.2876444.

One last neat feature is the use of the ifelse conditional statement which can be used to output text depending on the result of an R calculation:

> `r ifelse(sd(cars$speed) < 6, "The standard deviation is less than 6.", "The standard deviation is equal to or greater than 6.")`

The standard deviation is less than 6.

Note the use of > here, which signifies a quotation environment that will be indented.