---
title: "Getting Started with R"
date: "June 6, 2022"
output: html_document
---

## Install R and RStudio

To install **`R`** (latest release: 4.2.0), please go to <https://www.r-project.org> and choose your system. Click the *download R* link in the middle of the page under “Getting Started.” Download and install the installer files (executable, pkg, etc) that correspond to your system.

Although you can use `R` without any integrated development environment (IDE), you will need to install **RStudio**, by far the most popular IDE for `R`, for this summer. Basically, it makes your life with R much easier and we will be using it throughout the program.
To install RStudio, please go to <https://www.rstudio.com/products/rstudio/download/#download> and choose your system. The installer is preferred. If you have RStudio installed but not the latest version, just download the latest installer and install. 

## Typical workflow

**Follow the TA for a walkthrough of each component in  R Studio.**

### Writing R scripts

You can type commands directly into the **Console** (lower left pane), but this can become quite tedious and annoying when your work becomes more complex. Instead, you will be spending most of your time writing code in **R Scripts**. An R Script is a file type which R recognizes as storing R commands and is saved as a **.R** file. R Scripts are useful as we can edit our code before sending it to be run in the console. You can create a new R Script by clicking on the top left symbol in RStudio and selecting **R Script**.

### Using R Markdown

An R Markdown(`.Rmd`) file is a dynamic document combining the `R` and the markdown. It contains the reproducible `R` code along with the narration that a reader needs to understand your work. (This file itself is generated by R Markdown.) If you are familiar with the laTeX syntax, math mode works like a charm in almost the same way:

$$
f (x) = \frac{1}{\sqrt{2\pi}} \exp \left( - \frac{x^2}{2} \right)
$$

The chunks of embedded `R` codes are: 

```{r}
#R codes here
print("Hello World")
```

Running a Rmarkdown file and converting it into a reader-friendly documents require the `rmarkdown` and `knitr` package. All the lab documents will be Rmarkdown files so you need to know how to knit them. We recommend to knit as `html` file but if you have LaTex installed, you can knit as PDF. 

For more detailed information, [R Markdown Cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf) and the [online book](https://bookdown.org/yihui/rmarkdown/) are helpful. (For RStudio users, easily accessible from Help -> Cheatsheets)


Alternatively, you can create a **R Notebook** file which is similar to R Markdown, but instead allows you to knit code chunks separately without having to re-knit (aka re-run!) the entire file. Notebooks will only render HTML files, but can they be very useful for simultaneously coding, generating results, and making a presentable file to others.

## Install R packages

`R` performs a wide variety of functions, such as data manipulation, modeling, and visualization. The extensive code base beyond the built-in functions are managed by **packages** created from numerous statisticians and developers. The Comprehensive R Archive Network (CRAN) manages the open-source distribution and the quality control of the `R` packages.

To install a `R` package, using the function `install.packages` and put the package name in the parentheses and the quote. While this is preferred, for those using RStudio, you can also go to "Tools" then "Install Packages" and then input the package name.

```{r, eval = FALSE}
install.packages("tidyverse")
```

**Important Note**: NEVER install new packages in a code block in an .Rmd file. Always install new packages at the command line / Console. That is, the `install.packages()` function should NEVER be in your code chunks (unless they are commented out using #). The `library()` function, however, will be used throughout your code: The `library()` function loads packages only after they are installed.

If in any time you get a message says: "Do you want to install from sources the package which needs compilation?" Choose "No" will tend to bring less troubles. (Note: This happens when the bleeding-edge version package is available, but not yet compiled for each OS distribution. In many case, you can just proceed without the source compilation.)

Each package only needs to be installed once. Whenever you want to use functions defined in the package, you need to *load* the package with the statement:

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

Here is a list of packages that we may need (but not limited to) in the following lectures and/or labs. Please make sure you can install all of them. If you fail to install any package, please update the `R` and RStudio first and check the error message for any other packages that need to install first. 

```{r, message=FALSE, eval = FALSE}
library(tidyverse)
library(devtools)
library(rmarkdown)
library(knitr)
library(ranger)
library(glmnet)
```

## Basic data type and operators

### Data type: Vector

The basic unit of `R` is a vector. A vector `v` is a collection of values of the same type and the type could be:

1. numeric (double/integer number): digits with optional decimal point

```{r}
v1 <- c(1, 5, 8.3, 0.02, 99999)
typeof(v1)
```

2. character: a string (or word) in double or single quotes, "..." or ’...’.

```{r}
v2 <- c("apple", "banana", "3 chairs", "dimension1", ">-<")
typeof(v2)
```

3. logical: TRUE and FALSE

```{r}
v3 <- c(TRUE, FALSE, FALSE)
typeof(v3)
```

Note: Oftentimes, `factor` is used to encode a character vector into unique numeric vector.
```{r}
player_type <- c("Batter", "Batter", "Hitter", "Batter", "Hitter")
player_type <- factor(player_type)
str(player_type)
typeof(player_type)
```

### Data type: Lists

Vector can store only single data type:
```{r}
typeof(c(1, TRUE, "apple"))
```

**List** is a vector of vectors which can store different data types of vectors:
```{r}
roster <- list(
  name = c("Ron", "Nick", "Meg", "Wanshan", "Kenta", "YJ"),
  role = c("Instructor", "TA", "TA", "TA", "TA", "TA"),
  is_TA = c(FALSE, TRUE, TRUE, TRUE, TRUE, TRUE)
)

str(roster)
```

`R` uses a specific type of list, **data frame**, containing the same number of rows with unique row names.
```{r}
str(iris)
typeof(iris)
```

### Operators

We can preform element-wise actions on vectors through the operators:

1. Arithmetic: + - * / ^ (and, for integer division, %/% is quotient, %% is remainder)

```{r}
v1 <- c(1,2,3)
v2 <- c(4,5,6)

v1 + v2
v1 * v2

v2 %% v1
```

2. Relation: > >= < <= == != (last two are equals and is not equal to )

```{r}
5 > 4
5 <= 4
33 == 22
33 != 22
```

3. Logic: !(not) &(and) |(or)

```{r}
(5 > 6) | (2 < 3)
(5 > 6) & (2 < 3)
!(5 > 6) & (2 < 3)
```

4. Sequence: from:to (Colon operator)

```{r}
1:5
5:1
-1:-5
-1:5
```

## Read the csv file

Most of the data provided to you are in csv format. In the code chunk below, we use the [`read_csv()`](https://readr.tidyverse.org/reference/read_delim.html) function to load a dataset that is saved in a folder located in the SURE website. In quotations `"file_path"` is the file path where the dataset is located, which in this case is online. However, typically you'll save csv files locally first and put them in an organized folder to access later.

```{r, eval = FALSE}
heart_disease <- read_csv("http://www.stat.cmu.edu/cmsac/sure/2022/materials/data/health/intro_r/heart_disease.csv")
head(heart_disease)
```

## Looking for help

If you have any R problem, the best step is to use the `help()` function (or equivalently the `?`). For example, we can find out what the `help()` function does by typing `help(help)`:

```{r, eval = FALSE}
help(help)
```

Or you can use the statement "?" + "function name", for example:

```{r, eval = FALSE}
?help
```

Double question marks can lead to a more general search.

```{r, eval = FALSE}
??help
```

You should __ALWAYS__ consult the `R` help pages first before attempting to google around for a solution. 

## Exercise

1. Create four vectors, `v1` and `v2` are numeric vectors, `v3` is a character vector and `v4` is a logic vector. Make sure the length of `v1` and `v2` are the same. (Hint: a way to check the length is to use the function `length()`)

```{r}
#R code here
```

2. Preform add, minus, product and division on `v1` and `v2`.

```{r}
#R code here
```

3. Create four statements with both relation and logic operators, that 2 of them return `TRUE` and 2 of them return `FALSE`.

```{r}
#R code here
```

4. Create 2 sequences with length 20, one in an increasing order and the other in a decreasing order. 

```{r}
#R code here
```

5. Following is the `gapminder` dataset containing health and income outcomes for 184 countries from 1960 to 2016 from the `dslabs` package. How many of the rows in the dataset are from the Caribbean (coded as 'Caribbean' in `region`)? How about Eastern Europe? Can you neatly summarize the counts for all regions? (HINT: `table()`)

```{r}
library(dslabs)
data(gapminder)

#R code here
```


# OPTIONAL: Formating Text within RStudio

There are a lot of ways to format text within RStudio, e.g., *italics* and **bold** (just look at the .Rmd file to see how I did this). [See here](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf) for more tips/tricks on how to format things in R Markdown. As you'll see throughout this summer (and especially your project), well-formatted .html files can be a great way to showcase data science results to the public online.

# OPTIONAL: Customizing the RStudio User Interface

Within RStudio there are several panes that contain various things (Console, Help, Environment, History, Plots, etc). Here we discuss how you can customize how these panes are displayed.

If you're using Mac, go to RStudio / Preferences / Pane Layout. If you're using Windows, go to Tools / Global Options. Change the menu options to arrange the panes as you see fit. Click Apply and OK.

Now (still within the RStudio / Preferences menu), click Appearance and choose an appropriate font, font size, and theme. Click Apply and OK. Minimizing the bottom-left and bottom-right panes is a nice trick, which gives more vertical space to see your code and the output it's generating. (Minimize/maximize buttons are in the top-right of each pane.)

# OPTIONAL: Additional Customization Advice

+ Under Preferences / Code / Display, you might consider adding the margin column and setting it to 80 characters, since most style guides suggest that you should keep lines of code at 80 characters or less when possible.
+ You can set your background color, font, font size, etc. under Preferences / Appearance. (I use Cobalt). "Dark displays" are often easier on the eyes and are environmentally-friendly in that they conserve energy on devices. Of course, this is strictly a personal preference, and people can get unnecessarily dogmatic about it, so you should just choose something that you like.
+ Under Preferences / Packages, you can opt to change your CRAN mirror to the "Global (CDN) - RStudio" option, as it is very reliable.
+ Under Preferences / Git / SVN, you can configure your version control preferences. People often recommend using Git with RStudio for version control purposes. The interface is easy to use even if you're a beginner programmer or Git user. [This link](https://support.rstudio.com/hc/en-us/articles/200532077-Version-Control-with-Git-and-SVN) and [this link](https://jennybc.github.io/2014-05-12-ubc/ubc-r/session03_git.html) have more information on this, if you're interested.

# OPTIONAL: `R` Primers on RStudio Cloud

If you are struggling to install `R` and Rstudio on your computer, and/or having difficulties with installing the `tidyverse` then you __should make a free RStudio Cloud account at [`https://rstudio.cloud/`](https://rstudio.cloud/).__ This is a free, browser-based version of `R` and RStudio that also provides access to a growing number of `R` tutorials / primers relevant to this summer. 

After you create a RStudio Cloud account, click on the navigation menu by "Your Workspace". Then click on ["Primers"](https://rstudio.cloud/learn/primers) to bring up a menu of tutorials, with code primers you can choose to work through. RStudio Cloud is a great practical alternative to use __in case we are unable to resolve errors with regards to installation on your own personal computer__ (an unlikely scenario). We strongly encourage you to use an installed version of `R` and RStudio throughout the summer, due to RStudio Cloud data limitations that are important for your projects.


