Section 14 Basics of R Markdown

R Markdown creates dynamic documents, reports, presentation in HTML, PDF or Word. It integrates texts and outputs from multiple programming languages including R and Python.

R Markdown has the philosophy of reproducible research where you can weave the narrative and programming seamlessly in a reproducible manner.

For a comprehensive account on R Markdown, check the R Markdown: The Definitive Guide written by the authors who developed it.


14.1 R Markdown

  • R Markdown files take the extension .Rmd

  • RStudio recongises the R Markdown files and includes Knit option to prepare the document.

  • R Markdown document includes texts and chunks of R codes.

  • A simple presentation of R code chunks: {r echo=FALSE}

  • It uses backticks ``` to start and end code chunks

  • R Markdown uses two R Packages to prepare the Markdown document

    + `knitr`, a package that that executes the chunks of R code and turns R Markdown files (.Rmd) into markdown files (.md)
    
    + `pandoc` a package that turns markdown files into standard text output, e.g. HTML, PDF or Microsoft Word.
  • R Markdown also uses YAML (YAML Ain’t Markup Language) to present the header file.


14.2 R Markdown Quick Reference

You can use RStudio Help menu to find a snapshot of the following R Markdown Quick Reference.

Emphasis

*italic*   **bold**

_italic_   __bold__

Headers

# Header 1

## Header 2

### Header 3

Lists

Unordered List
* Item 1
* Item 2
    + Item 2a
    + Item 2b


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

Manual Line Breaks

End a line with two or more spaces:
Roses are red,   
Violets are blue.

Links

Use a plain http address or add a link to a phrase:

http://example.com

[linked phrase](http://example.com)

Images

Images on the web or local files in the same directory:

![alt text](http://example.com/logo.png)

![alt text](figures/img.png) 

Blockquotes

A friend once said:

> It's always better to give 
> than to receive.

R Code Blocks

R code will be evaluated and printed


``` r
summary(cars$dist)
summary(cars$speed)
```

Inline R Code

There were `r nrow(cars)` cars studied

Plain Code Blocks

Plain code blocks are displayed in a fixed-width font but not evaulated

```
This text is displayed verbatim / preformatted
```

Inline Code

We defined the `add` function to 
compute the sum of two numbers.

LaTeX Equations

Inline Equation

$equation$

Display Equation

$$ equation $$

Horizontal Rule / Page Break

Three or more asterisks or dashes:

******

------

Tables

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

Reference Style Links and Images

Links

A [linked phrase][id]. 

At the bottom of the document:

[id]: http://example.com/ "Title"

Images

![alt text][id]

At the bottom of the document:

[id]: figures/img.png "Title"

Miscellaneous

superscript^2^

subscript~2~

~~strikethrough~~