---
title: "Week 2 Resources"
subtitle: "PA 470 Spring 2024"
author: "Eric Langowski"
output:
  html_document:
    toc: true
    toc_float: true
    df_print: paged
    theme: sandstone
    number_sections: true
    code_folding: hide
---

```{r initial_setup, include=FALSE}
library(tidyverse)
library(lubridate)
library(kableExtra)
library(scales)
library(sf)
library(tigris)
library(tidycensus)
library(leaflet)
library(mapview)

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```


This file is a template for R Markdown and includes some resources mentioned in week 2.

- [Census Reporter](https://censusreporter.org/) to view data from the Census/ACS and identify appropriate geographic levels
- [sf cheat sheet](https://github.com/rstudio/cheatsheets/blob/main/sf.pdf) to view common sf functions. Generally, the only operations we are interested in are joins (`st_join`) and contains (`st_contain`). `st_intersection` also returns the spatial subset of geometries which are in both x and y.
- [color palettes](https://github.com/EmilHvitfeldt/r-color-palettes) for reference
- [Report](https://erhla.github.io/UCPD/ucpd_facts.html) and [code](https://github.com/erhla/UCPD/blob/master/ucpd_facts.Rmd) example of using R Markdown to communicate data online
- [census geography hierarchy](https://www2.census.gov/geo/pdfs/reference/geodiagram.pdf)

Leaflet map for Part 2

```{r, eval=FALSE}
#note that this chunk has eval=FALSE meaning it will not be evaluated

#create your palette (e.g. how you want to fill in the domain/target variable for mapping)
pal1 <-
  colorNumeric(
    palette = "Oranges",
    domain = tibble$targ_column_name,
    na.color = "Grey"
  )

#create your labels
#update the label_template as appropriate, Mbr> is a linebreak tag, <strong> is a bold tag
label_template <- str_glue("<strong>NAME %s</strong><br>TARG COLUMN (Pct): %s<br/>")

labels <- sprintf(
  label_template,
  tibble$NAME, #the first '%s'
  percent(tibble$targ_column_name, accuracy = .1) #the second '%s'
                ) %>% 
  lapply(htmltools::HTML)

your_map <- leaflet() %>%
  addTiles() %>% 
  addPolygons(
    data = tibble, #update this with your tibble name
    fillColor = ~ pal1(targ_column_name), #update this with the name of your target column
    weight = 0.5,
    opacity = 0.5,
    color = "white",
    dashArray = 3,
    fillOpacity = 0.7
     highlight = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    )
  ) %>%
  addLegend(
    pal = pal1, #the name of your palette
    values = tibble$targ_column_name, #the name of your target variable
    opacity = 0.7,
    title = NULL,
    position = "bottomright"
  )
```

Calculating Distance for Part 3

12.4.7 calculates distance matrices, e.g. the distance between two sets of points. In this case, the example distance of a ride is between two points, ride start and ride end. Since `sf` objects have one geometry this doesn't fit nicely into the patterns we saw in class. Here's a few steps breaking down the problem. Try to see if you can complete the problem without looking at all the steps.

Modifying `divvy` to have two points per row

This is a bit of a weird pattern. We want to apply functions on the rows of the tibble not of the column. In other languages we might use `apply` but in R it is often easier to use `rowwise` which groups a tibble by its rows so every mutation to the tibble is then applied to one row at a time.

```{r}
if(str_detect(getwd(), 'static')){
  file_loc <- '../data/'
} else {
  file_loc <- 'static/data/'
}

divvy_example <- read_csv(str_glue(file_loc, 
                          'week2_divvy_10rows.csv'))
```


```{r}
divvy_rowwise <- divvy_example %>% select(contains('lat'), contains('lng')) %>%
  rowwise()

divvy_rowwise %>% glimpse()
```

Then we need to create a simple feature geometry list column e.g. a column which is `start_point` and `end_point` which stores the lat/lon information as an `sf` object.

The functions you will need include `st_point` and `st_sfc` which takes a simple feature geometry (e.g. the output of `st_point`) and also has argument `crs` which should be 4326.

Finally, you can use `st_distance` to calculate the distance between `start_point` and `end_point`
