library(shiny)
library(leaflet)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Load data
Chicagoboundary <- st_read("CommAreas")
## Reading layer `CommAreas' from data source
## `/Users/caraoflaherty/OneDrive - The University of Chicago/GIS FINAL PROJECT/CommAreas'
## using driver `ESRI Shapefile'
## Simple feature collection with 77 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 1091131 ymin: 1813892 xmax: 1205199 ymax: 1951669
## Projected CRS: NAD83 / Illinois East (ftUS)
Socioeconomicindicators <- st_read("Socioeconomic Indicators Chicago NEW.csv")
## Reading layer `Socioeconomic indicators Chicago NEW' from data source
## `/Users/caraoflaherty/OneDrive - The University of Chicago/GIS FINAL PROJECT/Socioeconomic indicators Chicago NEW.csv'
## using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
# Assuming Chicagoboundary is a data frame
# Converting to sf and transform the projection
Chicagoboundary_sf <- st_as_sf(Chicagoboundary, coords = c("longitude", "latitude"), crs = 4326) %>%
st_transform(4326)
# Convert the COMMUNITY.AREA.NAME column to uppercase in Socioeconomicindicators
Socioeconomicindicators$COMMUNITY.AREA.NAME <- toupper(Socioeconomicindicators$COMMUNITY.AREA.NAME)
# Rename the COMMUNITY column in Chicagoboundary_sf
Chicagoboundary_sf$COMMUNITY.AREA.NAME <- Chicagoboundary_sf$COMMUNITY
Chicagoboundary_sf <- Chicagoboundary_sf[, !names(Chicagoboundary_sf) %in% "COMMUNITY"]
# Convert the COMMUNITY.AREA.NAME column to uppercase in Chicagoboundary_sf
Chicagoboundary_sf$COMMUNITY.AREA.NAME <- toupper(Chicagoboundary_sf$COMMUNITY.AREA.NAME)
Chicagoboundary_sf <- left_join(Chicagoboundary_sf, Socioeconomicindicators, by = "COMMUNITY.AREA.NAME")
# Join Socioeconomicindicators to Chicagoboundary by the COMMUNITY column
Chicagoboundary_sf$HARDSHIP.INDEX <- as.numeric(Chicagoboundary_sf$HARDSHIP.INDEX)
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
# Remove rows with NAs in HARDSHIP.INDEX
Chicagoboundary_sf_filtered <- Chicagoboundary_sf[complete.cases(Chicagoboundary_sf$HARDSHIP.INDEX), ]
# Leaflet map
output$map <- renderLeaflet({
colorPalette <- colorBin("YlOrRd", domain = Chicagoboundary_sf_filtered$`HARDSHIP.INDEX`, bins = c(0, 15, 30, 45, 60, 85, 100))
leaflet() %>%
addProviderTiles("Esri.WorldStreetMap") %>%
setView(lng = -87.6298, lat = 41.8781, zoom = 10) %>%
addPolygons(
data = Chicagoboundary_sf_filtered,
fillOpacity = 0.8,
color = "white",
weight = 1,
fillColor = ~colorNumeric("YlOrRd", Chicagoboundary_sf_filtered$HARDSHIP.INDEX)(HARDSHIP.INDEX),
highlight = highlightOptions(
weight = 3,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE
),
label = ~paste("Community Area: ", COMMUNITY.AREA.NAME , "<br/>HARDSHIP INDEX: ", HARDSHIP.INDEX),
labelOptions = labelOptions(noHide = TRUE, textOnly = TRUE, direction = "auto", style = list("opacity" = 0.7, "fill" = "white"))
) %>%
addLegend(
position = "bottomright",
pal = colorPalette,
values = Chicagoboundary_sf_filtered$`HARDSHIP.INDEX`,
title = "HARDSHIP INDEX",
labFormat = labelFormat(suffix = ""),
opacity = 0.7
)
})
}
shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents
print("Unique values in Chicagoboundary_sf$COMMUNITY.AREA.NAME:")
## [1] "Unique values in Chicagoboundary_sf$COMMUNITY.AREA.NAME:"
print(unique(Chicagoboundary_sf$COMMUNITY.AREA.NAME))
## [1] "DOUGLAS" "OAKLAND" "FULLER PARK"
## [4] "GRAND BOULEVARD" "KENWOOD" "LINCOLN SQUARE"
## [7] "WASHINGTON PARK" "HYDE PARK" "WOODLAWN"
## [10] "ROGERS PARK" "JEFFERSON PARK" "FOREST GLEN"
## [13] "NORTH PARK" "ALBANY PARK" "PORTAGE PARK"
## [16] "IRVING PARK" "DUNNING" "MONTCLARE"
## [19] "BELMONT CRAGIN" "WEST RIDGE" "HERMOSA"
## [22] "AVONDALE" "LOGAN SQUARE" "HUMBOLDT PARK"
## [25] "WEST TOWN" "AUSTIN" "WEST GARFIELD PARK"
## [28] "EAST GARFIELD PARK" "NEAR WEST SIDE" "NORTH LAWNDALE"
## [31] "UPTOWN" "SOUTH LAWNDALE" "LOWER WEST SIDE"
## [34] "NEAR SOUTH SIDE" "ARMOUR SQUARE" "NORWOOD PARK"
## [37] "NEAR NORTH SIDE" "LOOP" "SOUTH SHORE"
## [40] "CHATHAM" "AVALON PARK" "SOUTH CHICAGO"
## [43] "BURNSIDE" "CALUMET HEIGHTS" "ROSELAND"
## [46] "NORTH CENTER" "PULLMAN" "SOUTH DEERING"
## [49] "EAST SIDE" "WEST PULLMAN" "RIVERDALE"
## [52] "HEGEWISCH" "GARFIELD RIDGE" "ARCHER HEIGHTS"
## [55] "BRIGHTON PARK" "MCKINLEY PARK" "LAKE VIEW"
## [58] "BRIDGEPORT" "NEW CITY" "WEST ELSDON"
## [61] "GAGE PARK" "CLEARING" "WEST LAWN"
## [64] "CHICAGO LAWN" "WEST ENGLEWOOD" "ENGLEWOOD"
## [67] "GREATER GRAND CROSSING" "LINCOLN PARK" "ASHBURN"
## [70] "AUBURN GRESHAM" "BEVERLY" "WASHINGTON HEIGHTS"
## [73] "MOUNT GREENWOOD" "MORGAN PARK" "OHARE"
## [76] "EDGEWATER" "EDISON PARK"
print("Unique values in Chicagoboundary_sf$HARDSHIP.INDEX:")
## [1] "Unique values in Chicagoboundary_sf$HARDSHIP.INDEX:"
print(unique(Chicagoboundary_sf$HARDSHIP.INDEX))
## [1] 47 78 97 57 26 17 88 14 58 39 25 11 33 53 35 34 28 NA 70 46 71 42 23 85 10
## [26] 73 92 83 15 87 20 96 76 7 82 21 1 3 55 60 41 75 79 38 52 6 51 65 64 62
## [51] 98 44 32 67 84 61 5 43 91 69 93 29 56 80 89 94 66 2 37 74 12 16 30 19 8