---
title: "Lab 14.1 - Restaurant &amp; Health Codes"
author: "Author Goes Here"
date: "Semester Goes Here"
output: html_document
---

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

The goal of this exercise is to visualize health code violations in New York City restaurants.

🚧
Following the instructions given in Section 18.6, Problem 1.

We need to limit the number of addresses we geocode because they take around 1 second each to process.

```{r}
library(mdsr)
library(tidygeocoder)

violations_selected <- Violations %>%
  count(camis, dba, boro, building, street, zipcode) %>%
  filter(n > 90)  # n > 70 worked in spring 2022, but it's blowing out in spring 2023 (during the day).

violations_selected
```

Here we compute the geo-coordinates, filtering out an outlier that's actually in Rochester, NY.

```{r}
library(sf)

violations_geom <- violations_selected %>%
  mutate(address = paste(building, ", ", street, ", ", boro, ", NY,", zipcode)) %>%
  tidygeocoder::geocode(address, method = "osm") %>%
  na.omit() %>%
  filter(lat < 41.0) %>%
  st_as_sf(coords = c("long", "lat")) %>%
  st_set_crs(4326)  
violations_geom
```


```{r}
library(ggspatial)

violations_geom %>%
  ggplot() +
  aes(size = n) +
  annotation_map_tile(zoomin = 0, type = "osm") +
  geom_sf()
```

Now, we put them on an interactive map.

```{r}
library(leaflet)

violations_geom %>%
  mutate(popup = paste(dba, address, paste("violations:", n), sep = "<br/>")) %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(popup = ~popup)
```



