---
title: "Lead hazards in Syracuse, NY"
output:
flexdashboard::flex_dashboard:
theme: readable
social: menu
orientation: columns
vertical_layout: fill
source_code: embed
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
library(flexdashboard)
library(sf)
library(tidyverse)
library(leaflet)
library(leaflet.extras)
# lead violations
# updated by SYR open data on 1/19/2020
lead_viol <- sf::read_sf("https://opendata.arcgis.com/datasets/9870ad89e2af406eb48e127af1429f09_0.geojson")
map_data <- lead_viol %>%
mutate(lubridate::year(case_open_date),
complaint = stringr::str_trim(nature_of_complaint),
complaint = str_replace_all(complaint, "\r\n" , " "),
vacant = ifelse(is.na(vacant_property), "No", "Yes"),
case_open_date = lubridate::as_date(case_open_date)) %>%
select(#TNT = TNT_NAME, removed from dataset
Address = property_address,
Vacant = vacant,
Neighborhood = neighborhood,
Violation = complaint,
Case = case_number,
Opened = case_open_date,
Status = case_status,
Owner = property_owner_name
)
# parcel data
# updated by SYR open data on 2/11/20
# used query url to filter VacantBuil == Y
vacants_query <- sf::read_sf("https://services6.arcgis.com/bdPqSfflsdgFRVVM/arcgis/rest/services/Parcel_Map_Q1_2020/FeatureServer/0/query?where=VacantBuil%20%3D%20'Y'&outFields=*&outSR=4326&f=json")
vacants <- vacants_query %>%
mutate(year = readr::parse_number(YearBuilt)) %>%
filter(year < 1979) %>%
select(ADDRESS, YearBuilt, BLDG_STYLE,
Owner, TNT, IPSConditi,
Neighborho, n_OpenViol, Occupancy) %>%
st_centroid() # convert from polygon to point
isTRUE(st_crs(map_data) == st_crs(vacants))
# will add to leaflet to find center of all points
# based on the violations data
center <- map_data %>%
summarise() %>%
st_centroid() %>%
st_coordinates() %>%
as_tibble()
```
# Vacant Properties (built pre-1978)
Column {data-width=500}
-----------------------------------------------------------------------
### Property locator
```{r leaflet vacants}
vacants %>%
as_Spatial() %>%
leaflet() %>%
setView(lng = center$X,
lat = center$Y,
zoom = 12) %>%
addTiles(group = "Map") %>%
addProviderTiles(provider = "Esri.WorldImagery",
group = "Satellite") %>%
addCircles(label = ~ADDRESS,
color = "tomato",
group = "Points") %>%
addHeatmap(blur = 20,
max = 0.25,
radius = 10,
gradient = "OrRd",
group = "Density") %>%
addLayersControl(
baseGroups = c("Map", "Satellite"),
overlayGroups = c("Points", "Density"),
position = "bottomleft",
options = layersControlOptions(collapsed = FALSE)
) %>%
hideGroup("Density")
```
Column {data-width=500}
-----------------------------------------------------------------------
### Property details
```{r vacant datatable}
vacants %>%
st_set_geometry(NULL) %>%
select(TNT,
Address = ADDRESS,
`Year Built` = YearBuilt,
Neighborhood = Neighborho,
`Open Violations` = n_OpenViol,
Style = BLDG_STYLE,
`Property use` = Occupancy,
Condition = IPSConditi,
Owner) %>%
mutate_at(vars(
TNT, Neighborhood,
Condition, Style,
`Property use`,
Owner),
as.factor) %>%
DT::datatable(rownames = FALSE,
filter = "top",
extensions = "Buttons",
options = list(
autoWidth = TRUE,
columnDefs = list(
list(width = '180px',
targets = c(1, 6, 8)),
list(width = '50px',
targets = c(4, 7))
),
dom = 'Bfrtip',
buttons = c('csv', 'excel', 'print')
),
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: right;',
'Property details as of 2/11/2020. Source: data.syrgov.net')
)
```
# Open OCHD Lead Violations
Column {data-width=500}
-----------------------------------------------------------------------
### Open case locator
```{r leaflet violations}
map_data %>%
as_Spatial() %>%
leaflet() %>%
setView(lng = center$X,
lat = center$Y,
zoom = 12) %>%
addTiles(group = "Map") %>%
addProviderTiles(provider = "Esri.WorldImagery",
group = "Satellite") %>%
addCircles(label = ~Address,
group = "Points") %>%
addHeatmap(blur = 20,
max = 0.25,
radius = 10,
gradient = "BuPu",
group = "Density") %>%
addLayersControl(
baseGroups = c("Map", "Satellite"),
overlayGroups = c("Points", "Density"),
position = "bottomleft",
options = layersControlOptions(collapsed = FALSE)
) %>%
hideGroup("Density")
```
Column {data-width=500}
-----------------------------------------------------------------------
### Case details
```{r case datatable}
map_data %>%
st_set_geometry(NULL) %>%
mutate_at(vars(
Vacant, Neighborhood,
Case, Status, Owner),
as.factor) %>%
DT::datatable(rownames = FALSE,
filter = "top",
extensions = "Buttons",
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '130px',
targets = c(0,2,5)),
list(width = '280px',
targets = c(3))),
dom = 'Bfrtip',
buttons = c('csv', 'excel', 'print')
),
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: right;',
'Case details as of 1/19/2020. Source: data.syrgov.net')
)
```