Row

Sample of Restaurants in the Bronx by Inspection Grade

Row

Average Most Recent Inspection Score by Cuisine Type in Manhattan

Count of Inspection Grades Received by NYC Borough

Inspection Grades Received Over Time Among Au Bon Pain Locations in Manhattan

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
---

```{r setup, include=FALSE}
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(leaflet)
library(tidygeocoder)
library(ggridges)

data("rest_inspec")
nycrest =
  rest_inspec %>%
  group_by(camis) %>%
  slice(which.max(as.Date(inspection_date, "%Y-%m-%d", tz = "America/New_York"))) %>%
  filter(boro == "BRONX") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  filter(critical_flag %in% c("Critical", "Not Critical")) %>%
  filter(grade %in% c("A", "B", "C"))

nycrest2 =
nycrest %>%
    mutate(address = str_c(building, " " , street, " New York, NY", " ", zipcode))

addresses = 
  nycrest2 %>%
  geocode(address = address, method = "census", verbose = TRUE)

addresses2_df =
  addresses %>%
  drop_na(lat) %>%
  mutate(restaurant_name = dba)

cuisinerating =
  rest_inspec %>%
  group_by(camis) %>%
  slice(which.max(as.Date(inspection_date, "%Y-%m-%d", tz = "America/New_York"))) %>%
  filter(boro == "MANHATTAN") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  filter(cuisine_description != "Not Listed/Not Applicable") %>%
  group_by(cuisine_description) %>%
  summarise(avg_score = round(mean(score),0))

gradeboro =
  rest_inspec %>%
  group_by(camis) %>%
  slice(which.max(as.Date(inspection_date, "%Y-%m-%d", tz = "America/New_York"))) %>%
  filter(grade %in% c("A","B","C")) %>%
  filter(boro != "Missing") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  group_by(boro) %>%
  count(grade, name = "count_grade")

time =
  rest_inspec %>%
  filter(grade %in% c("A","B","C")) %>%
  filter(boro == "MANHATTAN") %>%
  drop_na(score) %>%
  drop_na(grade) %>%
  filter(dba %in% c("AU BON PAIN")) %>%
  arrange(inspection_date) %>%
  mutate(name = str_c(building, " ", street)) 
```

Row {data-width=650}
-----------------------------------------------------------------------

### Sample of Restaurants in the Bronx by Inspection Grade

```{r, message=FALSE}
pal = colorFactor("viridis", NULL)

addresses2_df %>%
  leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(~long, ~lat, radius = 1, color = ~pal(grade), popup = ~restaurant_name) %>%
    addLegend("bottomright", pal = pal, values = ~grade,
    title = "Restaurant Grade",
    opacity = 1)
```


Row {.tabset}
-----------------------------------------------------------------------

### Average Most Recent Inspection Score by Cuisine Type in Manhattan

```{r, message=FALSE}
cuisineplot =
cuisinerating  %>%
plot_ly(
    x = ~cuisine_description, y = ~avg_score, type = "scatter", mode = "markers", size = 0.3,
                             color = ~avg_score, alpha = 0.9)  %>%
    layout(yaxis = list(title = list(text = "Average Score")), 
           xaxis = list(title = list(text = "Cuisine Description"), 
                        showticklabels = FALSE))

colorbar(cuisineplot, title = "Average Score") 
```

### Count of Inspection Grades Received by NYC Borough

```{r, message=FALSE}
gradeboro %>% 
  plot_ly(x = ~grade, y = ~count_grade, color = ~boro, type = "bar", colors = "viridis",
          facet_col = "boro") %>%
  layout(yaxis = list(title = list(text = "Count")), 
        xaxis = list(title = list(text = "Grade")))
```

### Inspection Grades Received Over Time Among Au Bon Pain Locations in Manhattan 

```{r, message=FALSE}
time  %>%
 plot_ly(
    x = ~inspection_date, y = ~score,
    type = "scatter",color = ~name, mode = "lines+markers", colors = "viridis") %>%
     layout(yaxis = list(title = list(text = "Score Received")), 
           xaxis = list(title = list(text = "Inspection Date")))
```