## R Markdown

Please read through everything and then try the exercises.

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

The way you can create a file like this in RStudio is: File → New File → R Markdown and then using the default or using a template.

Now we will install some packages! Note it may take ~5-10 minutes to run all these steps. **You need to run these steps in the order listed.**

### 0.1

First copy and paste the following code into the console (the lower left panel/pane) - **but do not copy the backticks**: <br>
`install.packages("remotes")`<br>
This code will install a package from CRAN called "remotes". <br>
You may be asked a question in the console when you do this. If so, answer by typing Yes into the console.

### 0.2

Next copy and paste the following code again into the console (the lower left panel/pane), **again do not copy the backticks**:  <br>
`library(remotes)` <br>
This code loads the remotes package into memory- in other words we can use the functions within the package.<br>
You may be asked a question in the console when you do this. If so, answer by typing Yes into the console.

### 0.3

Now install a package that the instructors made that is not on CRAN but on GitHub, by copy and pasting the code into the console. We still don't want to copy the backticks.<br>
`install_github("jhudsl/jhur")`<br>
You may be asked a question in the console when you do this. If so, answer by typing Yes into the console.


The gray area below is a code chunk that will set up our packages and data (this will not show up in the rendered report when we press knit). You can also run the code within the editor area by pressing the green play button. Don't worry right now about what the code is doing, we will cover this later. We just want you to get used to RStudio and RMarkdowns.

```{r setup, message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(jhur)
library(ggplot2)
library(dplyr)

long <- read_circulator_long()
## take just average ridership per day
avg <- long %>%
  filter(type == "Average")
# keep non-missing data
avg <- avg %>%
  filter(!is.na(number))
```

When you click the **Knit** button (at the top of RStudio), a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. 
Next we will show you some code - no worries about understanding it yet, we just want to get you familiar with where code goes. 

When code is in an RMarkdown file chunk, it is saved to a file. When it is in the console, it is not saved. 

The console is useful for installing packages like we just did, this is because we only need to do it once, so we don't usually need to save the code.

## Plotting some data

Here is code that will make a plot of the average daily ridership in Baltimore City for the Charm City Circulator: https://www.charmcitycirculator.com/ (again don't worry about the actually code much for now).  


Here we plot a few days. You can embed an R code chunk like this: Try pressing the green play button to see what happens. Make sure you have run the previous code chunk first by pressing the green play button in that chunk.

```{r plot_circ, out.width = "100%"}
# keep only some days
avg <- avg %>%
  filter(day %in% c("Monday", "Tuesday", "Friday", "Saturday"))

palette <- c(
  banner = "blue",
  green = "darkgreen",
  orange = "orange",
  purple = "purple"
)

ggplot(aes(x = date, y = number, colour = line), data = avg) +
  geom_line() +
  facet_wrap(~day) +
  scale_colour_manual(values = palette)
```

```{r out.width = "100%", label = '0response'}
# keep only some days
avg <- avg %>%
  filter(day %in% c("Monday", "Tuesday", "Friday", "Sunday"))

palette <- c(
  banner = "red",
  green = "darkgreen",
  orange = "orange",
  purple = "purple"
)

ggplot(aes(x = date, y = number, colour = line), data = avg) +
  geom_line() +
  facet_wrap(~day) +
  scale_colour_manual(values = palette)
```

When you click the **Knit** button (at the top of RStudio), a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. 
Next we will show you some code - no worries about understanding it yet, we just want to get you familiar with where code goes. 

When code is in an RMarkdown file chunk, it is saved to a file. When it is in the console, it is not saved. 

The console is useful for installing packages like we just did, this is because we only need to do it once, so we don't usually need to save the code.

## Exercise 

Here are a few changes that will show you how to change small things in `R` code and the output it makes.  After each change, hit the **Knit** button again.

Here is another code chunk for our exercises. 

```{r}
x <- c(1, 2, 3)
```

### 1.1

Go through the code for the plot above and change the colors in `palette` to something other than what they originally were. See http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf for a large list of colors. For example, you could replace blue with red.

### 1.2

Add a new R chunk below the bonus instructions.You can use the insert chunk button or copy paste an existing code chunk. Include a lowercase x inside the chunk as the code. Thus it should look like the chunk above but without `<- c(1,2,3)`. Make sure you press the knit button after this to see what the new chunk looks like.


# Practice on Your Own!

### P.1

Update the options of the R chunk you just made (with the lowercase x in it) so that the output option is `show output only`. How does the chunk look now?

### P.2

Create another R Markdown Document from RStudio dropdowns: File → New File → R Markdown.



```{r echo=FALSE, label = '2response'}
x
```
