I revised the original code to more closely reflect the code in the zipcode explorer app. Now, instead of displaying the values for all selected features, the popup displays nothing. However, the code throws no errors. How can I pass content to the label successfully?
#libraries and data source for tract layer
library(shiny)
library(sf)
library(tidyverse)
library(leaflet)
library(sp)
#read dataset from https://gis-kingcounty.opendata.arcgis.com/datasets/census-2000-tracts-major-wtrbdy-features-removed-tracts00-shore-area.
#Change DSN in st_read to match your directory path.
tracts_demo <- st_read(
dsn = "//dot/Transit/SD/ServicePlanning/Major Projects/Renton_Kent_Auburn_Area_Mobility_Plan/EIR/R/data/2016tract.shp",
stringsAsFactors = FALSE) %>%
st_transform(., crs = 4326)
#UI
ui <- fluidPage(
# Application title
titlePanel("Repex Demo"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("tracts",
"Select Tracts by Tract_ID:",
choices = as.list(tracts_demo$GEOID),
multiple = TRUE,
selectize = TRUE
)
),
mainPanel(
leafletOutput("map", height = "100vh")
)
)
)
Server code. This is the problematic piece of the code.
server <- function(input, output) {
tracts_reactive <-reactive({
tracts_demo %>%
filter( tracts_demo$GEOID %in% input$tracts)
})
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(lng = -122.217064, lat = 47.482880, zoom = 11 )
})
observe({
proxy<- leafletProxy("map")
%>% clearGroup("tracts")
proxy %>%
addPolygons(data= tracts_reactive(),
color = "#444444",
weight = 1,
smoothFactor = 0.5,
opacity = 1.0,
fillOpacity = 0.5,
fillColor = "Black",
group = "tracts")
})
#I think this code is the issue
showTractPopUp <- function(tracts, lat, lng) {
selectedTract <- tracts_demo[tracts_demo$GEOID == tracts,]
content <-as.character(selectedTract$GEOID)
leafletProxy("map") %>% addPopups(lng, lat, content, layerId = tracts)
}
observe({
leafletProxy("map") %>% clearPopups()
event <- input$map_shape_click
if(is.null(event))
return()
isolate({
showTractPopUp(event$id, event$lat, event$lng)
})
})
}
# Run the application
shinyApp(ui = ui, server = server)