I'm currently working on a Shiny Application that shows users where they can park a vehicle at a public university. Right now I'm trying to figure out why all of the polygons in my simple feature object display the same label "Lot L". I intended for each label to display the location name associated with the corresponding POLYGON (i.e. the "Eagle Bank Arena" Polygon is labeled "Eagle Bank Arena", the "Johnson Center" polygon is labeled "Johnson Center", and so on...)
The location names are contained in an attribute column called "Location". Any help as to how I can resolve my problem will be greatly appreciated.
Link to Shiny App:
https://cypher-trial.shinyapps.io/Trial_Map/
My Code:
library(shiny)
library(shinyWidgets)
library(tidyverse)
library(leaflet)
library(leaflet.extras)
library(htmltools)
library(sf)
library(ggmap)
#Joining Coordinates
Johnson_Center <- rbind(c(-77.30488,38.82845), c(-77.30422,38.82837), c(-77.30399,38.82942), c(-77.30466,38.82951), c(-77.30488,38.82845))
Shenandoah_Parking_Deck <- rbind(c(-77.30807,38.83035),c(-77.30746,38.83060),c(-77.30675,38.82959),c(-77.30741,38.82933),c(-77.30807,38.83035))
Eagle_Bank_Arena <- rbind(c(-77.30995,38.82731),c(-77.31029,38.82702),c(-77.31028,38.82664),c(-77.30989,38.82638),c(-77.30940,38.82641),c(-77.30906,38.82670),c(-77.30908,38.827076),c(-77.30946,38.82733),c(-77.30995,38.82731))
Rappahannock_Parking_Deck <- rbind(c(-77.30673,38.83415),c(-77.30687,38.83509),c(-77.30521,38.83526),c(-77.30504,38.83432),c(-77.30673,38.83415))
Mason_Pond_Parking_Deck <- rbind(c(-77.30998,38.82996),c(-77.31028,38.83043),c(-77.30954,38.83075),c(-77.30956,38.83079),c(-77.30943,38.83087),c(-77.30906,38.83036),c(-77.30998,38.82996))
Lot_L <- rbind(c(-77.31015,38.82494),c(-77.31042,38.82492),c(-77.31180,38.82593),c(-77.31118,38.82645),c(-77.30928,38.82513),c(-77.30978,38.82468),c(-77.31015,38.82494))
#Combine Coordinate into list of ploygons
Test_Locations <-st_polygon(list(Johnson_Center,
Shenandoah_Parking_Deck,
Eagle_Bank_Arena,
Rappahannock_Parking_Deck,
Mason_Pond_Parking_Deck,
Lot_L))
#Test the ploygons
plot(Test_Locations)
#Create Simple Feature Column
Test_Location_sfc <- st_sfc(Test_Locations)
#Create a dataframe with just one column - "Location"
Location_Name <- cbind.data.frame(c("Johnson Center",
"Shenandoah Parking Deck",
"Eagle Bank Arena",
"Rappahannock Parking Deck",
"Mason Pond Parking Deck",
"Lot L"))
names(Location_Name)[1]<-"Location"
#Create Simple Feature
sf_final <- st_sf(Location_Name, Test_Location_sfc)
#Define CRS
st_crs(sf_final) <- "+proj=longlat +datum=WGS84"
#Define palette for legend
simple_list <- c("Always","Sometimes","Never")
legend_options <- factor(simple_list, labels = c("Always","Sometimes","Never"))
pal_legend <- colorFactor(palette = c("#41d33f","#e3fc44","#838487"),
domain = legend_options,
ordered = FALSE)
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("MAP_EXAMPLE", width = "100%", height = "100%"),
absolutePanel(id = "controls", class = "panel panel-default",
fixed = TRUE, draggable = TRUE, top = 20, left = "auto", right = 20,
bottom = "auto", width = 330, height = "auto", align = "center",
p(h2("Where Can I Park?")),
br(),
pickerInput("permit_or_visitor",label = "Are you a permit holder or visitor?", choices = c('Active Permit Holder','Visitor'),
multiple = FALSE, selected = 'Visitor', options = list(`actions-box` = TRUE,`none-selected-text` = "Nothing Selected",`live-search` = TRUE)),
conditionalPanel("input.permit_or_visitor == 'Active Permit Holder'",
pickerInput("permit_type",label = "Permit Type?", choices = c("Permit A","Permit B","Permit C","Permit D"),
multiple = FALSE, selected = "Permit A", options = list(`actions-box` = TRUE,`none-selected-text` = "Nothing Selected",`live-search` = TRUE))),
conditionalPanel("input.permit_or_visitor == 'Visitor'",
pickerInput("visitor_type",label = "What type of parking are looking for?", choices = c("Option A","Option B","Option C","Option D"),
multiple = FALSE, selected = "Option A", options = list(`actions-box` = TRUE,`none-selected-text` = "Nothing Selected",`live-search` = TRUE)))
)
)
server <- function(input, output) {
output$MAP_EXAMPLE <- renderLeaflet({
leaflet(options = leafletOptions(dragging = TRUE,
minZoom = 15,
maxZoom = 19)) %>%
addProviderTiles("OpenStreetMap") %>%
setView(lng = -77.3084807, lat = 38.8314936, zoom = 15) %>% #Complete Zoom Range [0:19]
setMaxBounds(lng1 = -77.2800,
lat1 = 38.8210,
lng2 = -77.3380,
lat2 = 38.8415) %>%
addResetMapButton() %>%
addMarkers(lng = -77.280, lat = 38.821, popup = "Bottom Right") %>%
addMarkers(lng = -77.3380, lat = 38.8415, popup = "Top Left") %>%
addPolygons(data = sf_final,
color = "purple",
label = ~Location) %>%
addLegend(title = "Can I park here?",
position = "bottomleft",
pal = pal_legend,
values = legend_options,
opacity = 1.0)
})
}
# Run the application
shinyApp(ui = ui, server = server)