---
title: "Intro to R Notebooks"
output: html_notebook
---

# About this R Notebook

R Notebooks are a 'flavor' of R markdown that combine plain text with R commands in code chunks. You can (and should!) edit them with your own notes. Don't forget to save every few minutes, like anything else!

You can use markdown characters to format text as **bold**, *italic*, or ~~strikethrough~~.

R codes are embedded in 'code chunks'. When you run the code, the output appears in the notebook under the code chunk (this can be changed under Settings).

To view a copy of your notebook as HTML, just hit **Save**, then **Preview**.

Keyboard shortcuts:
 - run the current line of R: *ctrl + enter*
 - run everything in the current code chunk: *ctrl + shift + enter*
 - insert a new code chunk: *ctrl + alt + i*

# Run Code

```{r}
x = "Hello World!"
x
```

```{r}
LETTERS[1:5]
```

# YOUR TURN

Compute the product of the integers from 1 to 5. [Answer](https://bit.ly/3rnlOaY)

```{r}
# Your answer here
```

# Plot a Cosine Wave

Run the following:

```{r}
## Plot a cosine wave
xvals = 4 * pi * (0:100)/100
plot(x = xvals, y = cos(xvals), type = "b")
```

# YOUR TURN

Plot a sine wave. [Answer](https://bit.ly/36NPmp2)

```{r}
# Your answer here
```

# Plot the Batman Logo

Run the following lines all at once:

```{r}
## Plot the batman logo
f1u <- function(x) ifelse ((abs(x) >  3 & abs(x) <= 7), 3*sqrt(1-(x/7)^2), 0)
f1d <- function(x) ifelse ((abs(x) >= 4 & abs(x) <= 7), -3*sqrt(1-(x/7)^2), 0)
f2u <- function(x) ifelse ((abs(x) > 0.50 & abs(x) < 0.75), 3*abs(x)+0.75, 0)
f2d <- function(x) ifelse ((abs(x) > -4 & abs(x) < 4), abs(x/2)-(3*sqrt(33)-7)*x^2/112-3 + sqrt(1-(abs(abs(x)-2)-1)^2), 0)
f3u <- function(x) ifelse ((x > -0.5 & x < 0.5), 2.25, 0)
f4u <- function(x) ifelse ((abs(x) >  1 & abs(x) <= 3), 6 * sqrt(10)/7 + (1.5 - 0.5 * abs(x)) * sqrt(abs(abs(x)-1)/(abs(x)-1)) - 6 * sqrt(10) * sqrt(4-(abs(x)-1)^2)/14, 0)
f5u <- function(x) ifelse ((abs(x) >= 0.75 & abs(x) <= 1), 9-8*abs(x), 0)
fu <- function (x) f1u(x) + f2u(x) + f3u(x) + f4u(x) + f5u(x)
fd <- function (x) f1d(x) + f2d(x)
bm <- function(r,x) ifelse(r%%2==0, fu(x), fd(x))
opar <- par(bg="#02233f", mar=c(0,0,0,0))
plot(x=seq(-7,7,0.065), y=Vectorize(bm)(1:216, seq(-7,7,0.065)), col="#999900", type="l", lwd=3)
par(opar)
```

# View the Attendee Map

```{r}
library(googlesheets4)
library(sf)
library(leaflet)

sheet_id <- "1lu2PBiKCK_A628ADY9opgpeqGmg0kc8JsV63KCYmLzo"

## Read the data frame from Google Sheets
gs4_deauth()
pipo_df <- range_read(sheet_id, sheet = "Form Responses 1")

## Convert the data frame to a simple features data frame
pipo_sf <- st_as_sf(pipo_df, coords = c("Lon", "Lat"), crs = 4326)

## Plot the locations in an interactive leaflet map
leaflet(pipo_sf) %>% 
  addTiles() %>% 
  addCircleMarkers(radius = 5, color = "red", opacity = 0.5, 
                   popup = ~paste0("<p><b>", `First Name`, "</b></p>",
                                   "Favorite plant / animal: ", `Favorite plant or animal`, "</p>"))
```

# End

Congratulations on finishing the Notebook! Save it (again).

Click on the 'Preview' button in the RStudio toolbar to save a copy of your work as HTML.


