---
#############################################################
#                                                           #
# In RStudio click on "Run Document" to run this tutorial   #
#                                                           #
#############################################################
title: "A Brief Overview of R"
author: "Luke Tierney"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)
library(tidyverse)
knitr::opts_chunk$set(echo = FALSE, comment = "", warning = FALSE)
```

```{r stop_when_browser_closes, context = "server"}
# stop the app when the browser is closed (or, unfortunately, refreshed)
session$onSessionEnded(stopApp)
```

## Basic Usage

Use the colon operator to create the sequence 3, 2, 1.

```{r down-colon, exercise = TRUE}

```
```{r down-colon-solution}
3 : 1
```

What happens if you add `(1 : 3)` and `(4 : 5)`?


```{r recycle-non-multiple, warning = TRUE, exercise = TRUE}

```
```{r recycle-non-multiple-hint}
(1 : 3) + (4 : 5)
```

## Data Frames

### Counting Rows and Columns

How many rows and columns does the `faithful` data frame have?

```{r faithful-nr, exercise = TRUE}

```
<div id = "faithful-nr-hint">
**Hint:** Try `nrow()`, `ncol()`, or `dim()`.
</div>


### Making New Data Frames

You can use `data.frame()` to create a data frame. For example:

```{r, echo = TRUE}
data.frame(x = 1 : 4, y = rnorm(4))
```

What happens if you try to create a data frame with variables that are
not all the same length?

```{r data-frame-uneq, exercise = TRUE}

```
```{r data-frame-uneq-hint}
data.frame(x = 1 : 4, y = rnorm(3))
```

## Functions

The sum of the integers $1, 2, \dots, n$ is given $n \times (n + 1) / 2$.

Write a function `nsum()` that takes as argument `n` and computed this value.

```{r intsum, exercise = TRUE}

```

```{r intsum-hint, eval = FALSE}
nsum <- function(n) ___
```

```{r intsum-solution}
nsum <- function(n) n * (n + 1) / 2
```

What happens when you evaluate `nsum(1 : 5)`?

```{r intsum-vec-setup}
nsum <- function(n) n * (n + 1) / 2
```
```{r intsum-vec, exercise = TRUE}

```

## The Tidyverse

The `mtcars` data set contains data on some (very old) car models.

```{r}
head(mtcars, 4)
```

Write a pipeline to compute the average `mpg` values for 4, 6, and 8
cylinder cars.

```{r cyl-mpg-setup}
library(dplyr)
```
```{r cyl-mpg, exercise = TRUE}

```
```{r cyl-mpg-hint, eval = FALSE}
mtcars |>
    group_by(___) |>
    summarize(avg_mpg = ___)
```


## Exercises

### Exercise 1

Compute the mean of the numbers 1, 3, 5, 8.

```{r simple_mean, exercise = TRUE}

```
```{r simple_mean-solution}
mean(c(1, 3, 5, 8))
```
```{r simple_mean-question, echo = FALSE}
question("The answer is closest to:",
  answer("5.75"),
  answer("4.25", correct = TRUE),
  answer("3.75"),
  answer("5.25"),
  random_answer_order = TRUE,
  allow_retry = TRUE
)
```


### Exercise 2

What is the mean of the `eruptions` variable in the `faithful` data
frame?

```{r eruptions_mean, exercise = TRUE}

```
```{r eruptions_mean-solution}
mean(faithful$eruptions)
```
```{r eruptions_mean-question, echo = FALSE}
question("The answer is closest to:",
  answer("3.35"),
  answer("3.49", correct = TRUE),
  answer("3.87"),
  answer("3.16"),
  random_answer_order = TRUE,
  allow_retry = TRUE
)
```


### Exercise 3

Find the average of the first 50 eruption durations in the faithful`
data frame.

```{r eruptions50_mean, exercise = TRUE}

```
```{r eruptions50_mean-solution}
mean(faithful$eruptions[1 : 50])
```
```{r eruptions50_mean-question, echo = FALSE}
question("The answer is closest to:",
  answer("2.50"),
  answer("3.30", correct = TRUE),
  answer("3.13"),
  answer("4.33"),
  random_answer_order = TRUE,
  allow_retry = TRUE
)
```


### Exercise 4

Use the `median` function to modify the pipe example in the tidyverse
section to include medians.

```{r add_median, exercise = TRUE}

```
```{r, add_median-hint, eval = FALSE}
faithful |>
    mutate(type = ifelse(eruptions < 3, "short", "long")) |>
    group_by(type) |>
    summarize(mean = mean(waiting),
              ___,
              sd = sd(waiting))
```
```{r add_median-solution}
faithful |>
    mutate(type = ifelse(eruptions < 3, "short", "long")) |>
    group_by(type) |>
    summarize(mean = mean(waiting),
              median = median(waiting),
              sd = sd(waiting))
```
