---
title: "RMarkdown Syntax - Cheatsheet"
author: 
  - name: "Katia Bulekova"
    affiliation: "Boston University"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

******

# Heading1 

## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6

*******************************

## Text Formatting

This is a plain text.  
If you want to start a new paragraph - add 2 space characters at the end of the line.

One star ( * ) or one underscore ( _ ) around the text make it *italic*.

Two stars or underscores - **bold**

superscript^27^    
subscript: H~2~O  
~~strikethrough~~  

[link description](https://www.bu.edu/)

*******************************


RMarkdown supports LaTeX syntax:  
A Math formula surrounded by a dollar sign $\sum_{i=1}^n X_i$ is inline with text.

When it is surrounded by two dollar signs, it is placed in a separate line:
$$\sum_{i=1}^{n}\left( \frac{X_i}{Y_i} \right)$$


Multiple starts (3 or more)  make a horizontal rule. Needs a blank line above and below:  

*******************************

First line.<br>
Second Line.  
Third line.  


## Lists
Lists (both ordered and unordered) need a blank line first:

* unordered list item
* next item in the unordered list
  + sub-item1
  + sub-item2
    + sub-item3
    + sub-item7
  
  
1. First item
5. Second Item
   a) sub-item1
   b) sub-item2
   e) sub-item3



********************************

## Tables

Table Header | Second Header
------------------|------------------
    cell 1         |    cell 2
cell 3         | cell 4


:--- text is left justified
:--: text is centered
---: text is right justified

Formatted Table example:

|             |  Sum Sq  |  Df    |    F value    |   Pr(>F)    |                 |
|:------------|---------:|-------:|--------------:|:-------------|:----------------|
|  *(Intercept)*|  42785   |  1     |  357.4672     | **< 2.2e-16**   | &#42;&#42;&#42; |
|  *var1*       |  451     |  1     |  3.7653       | 0.0582130     | .                | 
|  *var2*       |  2034    |  2     |  8.4980       | **0.0006926** | &#42;&#42;&#42; |

The number of "-" symbols for each column on the second line of the above table controls the column width in the output.


**Note:** It is much easier to generate this table using R code. We included it here just as an example of using RMarkdown syntax for table generation.

************

## R Code
```{r}
x<-3
```

R code can be included inline with a text using single back ticks and a lower case letter r. For example, we can define a variable x  and assign a value to it `r x <- 7` and then compute its square and use it in the text: `r x*x`.

```{r}
```

We can also include *code chunks*. This can be done by pressing a green button with a letter "C", or with the keyboard shortcut Ctrl + Alt + I (Command + Option + I):



```{r warning=FALSE, message=FALSE}
library(tidyverse)
```

By default, both R chunk and its output are included in the resulting document. We will also see error and warning messages (if any).

We can control what is included in the output document using chunk options:
```{r eval=FALSE }
x1 <- rnorm(100)
print(mean(x1))
```

A comprehensive description of the chunk options can be found in ["Dynamic Documents with R and knitr](https://yihui.org/knitr/options/#code-evaluation) by Yihui Xie.


Below is a list of some options:

Option   | Default Value   |  Description 
:--------|:------|:------------------------
eval           | TRUE       | Evaluate the code and include its results
echo           | TRUE       | Display code along with its results
warning        | TRUE       | Display warnings
error          | FALSE      | Display errors
message        | TRUE       | Display messages
tidy           | FALSE      | Reformat code in a tidy way when displaying it
results        | "markup"   | "markup", "asis", "hold", or "hide"
cache          | FALSE      | Whether to cache results for future renders
comment        | "##"       | Comment character to preface results with
fig.width      | 7          | Width in inches for plots created in chunk
fig.height     | 7          | Height in inches for plots created in chunk

The chunks option can be included on the first line, separated by a comma:

```{r eval=TRUE, warning=FALSE}
x1 <- rnorm(100)
print(mean(x1))
```

They can also be included into the chunk itself, using YAML format. Notice, that in the later case, we use column (:) symbol to assign the value and the logical value is specified using lower-case letters:

```{r}
#| eval: false
#| warning: true
x1 <- rnorm(100)
print(mean(x1))
```


#### results option
*results* option can take one of 4 values: “markup”, “asis”, “hold”, or “hide”.

Below is the output of a chunk for each choice:

*markup* (regular output):
```{r}
#| results: "markup"
x1 <- rnorm(100)
print(mean(x))
```

*asis* (text output):
```{r}
#| results: "asis"
x1 <- rnorm(100)
print(mean(x))
```

*hold* (hold all pieces of text output in a chunk and flush them to the end of the chunk):
```{r}
#| results: "hold"
x1 <- rnorm(100)
print(mean(x1))
x2 <- rnorm(100)
print(mean(x2))
```

*hide* (hide text output):
```{r}
#| results: "hide"
x1 <- rnorm(100)
print(mean(x))
```



