---
title: "Introduction to R Markdown"
author: "Your Name"
date: "8/26/2021"
output:
  html_document: default
  word_document: default
  pdf_document: default
---

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

## RStudio Intro

```{r}
print("This is Thursday.")
```


```{r RvsRStudio, eval = !knitr::is_latex_output(), echo = FALSE}
knitr::include_graphics("https://moderndive.netlify.app/images/shutterstock/R_vs_RStudio_1.png")
```

<!-- Markdown syntax for including images
![](https://moderndive.netlify.app/images/shutterstock/R_vs_RStudio_1.png)
-->

- If you're new to R, follow the online ModernDrive chapter https://moderndive.netlify.app/1-getting-started.html
- Four Panes:
    1. Source panel
    2. Console
    3. Global environment
    4. Files/Plots/Packages/Help/Viewer

### Recommended Options

```
Tools --> Global Options --> 
- Uncheck "Restore .RData into workspace at startup".
- Set "Save workspace to .RData on exit" to "Never".  
- (Optional) R Markdown --> Set "Show output preview in" to "Viewer Pane".

Use Project
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the **Knit** button 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:

> Note: R is case sensitive. So `Cars` and `cars` are different. 

```{r cars, results = 'hide'}
summary(cars[-(1:4), ])
```

### YAML options

- `title`, `author`, `date`, `output`

### Chunk options

- `include`
- `echo`
- `eval`
- `results = 'hide'`

## Including Plots

You can also embed plots, for example:

```{r pressure, echo = FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

## Install packages

```{r tweet-by-Di-Cook}
tweetrmd::include_tweet("https://twitter.com/visnut/status/1248087845589274624")
```

```{r change-a-light-bulb, eval = !knitr::is_latex_output(), echo = FALSE}
knitr::include_graphics("https://pbs.twimg.com/media/EVIZRvPU0AAwgdH?format=jpg")
```

Check out https://rladiessydney.org/courses/ryouwithme/01-basicbasics-2/

```{r install-tidyverse-lme4}
# Install the tidyverse "meta" package
# install.packages("tidyverse")
# Install the lme4 package
# install.packages("lme4")
```

## Load a package

> Tip 1: Try the (PC) <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>I</kbd>/(Mac) <kbd>Cmd</kbd> + <kbd>Option</kbd> + <kbd>I</kbd> shortcut for a new code chunk 

> Tip 2: Use <kbd>Tab</kbd> for code completeion

> Tip 3: Use <kbd>Ctrl</kbd> + <kbd>Enter</kbd>/(Mac) <kbd>Cmd</kbd> + <kbd>Return</kbd> for running a line of R code

> Tip 4: Set `message = FALSE` to suppress messages in loading packages

```{r load-package}
# Load tidyverse

# Load lme4

```

## Data Frame

```{r data-frame}
# Load SleepStudy

# Extract one column

# Extract column by index (same as last line)

# Extract two rows

# Compute the mean and sd, and chain them together

# Correlation matrix with psych::pairs.panel()

# Find out what a function does (use `?function_name`, e.g., `?pairs.panel`)

```

## Basic Markdown Elements

From RStudio, click `Help` --> `Markdown Quick Reference`

### Italic and bolded texts

This is *italic*

### Lists (Ordered and Unordered)

- Item 1
- Item 2

### Equations (LaTeX)

Inline: The correlation between $a = b + c + \tau$

Display: 

$$a = b + c + \tau$$

## Cheatsheet

More detailed cheatsheet: https://rmarkdown.rstudio.com/lesson-15.HTML

***

## Exercise

1. Download the Rmd file for the exercise on Blackboard
2. Insert your name on line 3.
3. Complete the following in this R Markdown document:
    a. Copy the following LaTex equation to below: `A_1 = \pi r^2`. How does this say about writing Greek letters and subscripts/superscripts?
        $$[Insert equation here]$$
    b. Install and then load the `modelsummary` package, and run the following. You'll need to remove `eval = FALSE` so that it runs. Find out what this code chunk does.
```{r modelsummary, indent = '        ', eval = FALSE}
# Install and load the modelsummary package first; otherwise it won't run
library(modelsummary)
fm1 <- lm(dist ~ speed, data = cars)
fm2 <- lm(dist ~ poly(speed, 2), data = cars)
fm3 <- lm(log(dist) ~ log(speed), data = cars)
msummary(list(fm1, fm2, fm3))
```
    c. Run the following. You'll need to remove `eval = FALSE` so that it runs. Find out what this code chunk does.
```{r scatter-plot, indent = '        ', eval = FALSE}
ggplot(cars, aes(x = log(speed), y = log(dist))) +
  geom_point() +
  geom_smooth()
```
    d. Add a code chunk below to show the output of running `sessionInfo()`, which prints out the session information of your computer. Make the code chunk to show only the output, but not the code.
    
    e. Knit the document to HTML, PDF, and Word. If you run into an error when
    knitting to any one of the formats, record the error message. Which format
    do you prefer?
    f. Go to the top of this Rmd file, and change the line inside YAML
    
        ```
          html_document: default
        ```
        
        to
        
        ```
          html_document: 
              toc: TRUE
        ```
        
        Knit the document again. What does it do?
    g. Submit the knitted document to Blackboard in your preferred format (HTML, PDF, or WORD)
