I have been able to create a shiny app that utilizes leaflet in order to display my data on a map at certain points on that map (see below image).
My code currently looks like that below. However my issue is, is that the popup (see image) only displays data for the last date in my data frame no matter what date I select on the slider. Currently, the popup data isn't affected, no matter what date I select on the slider. You can see that the popup says 2021-08-31 even though 2021-08-01 is selected.
I need to be able to use the slider to change the PM value and date that is displayed based on the date I select. Can someone help?
library(tidyverse)
library(leaflet)
library(lubridate)
library(shiny)
test_map <- leaflet(width = "100%") %>%
setView(lng = -123.2504, lat = 49.2652, zoom = 15) %>%
addProviderTiles("Esri.WorldStreetMap") %>%
addLegend(
position = c("topright"),
pal=pal,
values=AQ_df$severity,
title="<strong>PM2.5 (ug/m3)</strong>")%>%
addCircles(
data=AQ_df,
lng = ~Longitude,
lat = ~Latitude,
radius = 30,
color = 'black',
fillColor = ~pal(severity),
fillOpacity = 1,
weight = 1,
popup = paste0("<strong>ID: </strong>", AQ_df$RAMP_label, "</br>",
"<strong>Location: </strong>", AQ_df$RAMP_desc, "</br>",
"<strong>PM2.5 (ug/m3): </strong>", AQ_df$PM_RAMP, "</br>",
"<strong>Date: </strong>", AQ_df$date))
data <- AQ_df
ui <- shinyUI(pageWithSidebar(
headerPanel("Hello Shiny Leaflet with Date range!"),
sidebarPanel(
sliderInput(
"range",
"Date range",
min = as_date(startdatetime),
max = as_date(enddatetime),
value=as.Date(startdatetime)
)
),
mainPanel(
leafletOutput("map")
)
))
server <- shinyServer(function(input, output) {
filtered_data <- reactive({
data %>% filter(date > input$range[1] & date < input$range[2])
})
output$map <- renderLeaflet({
test_map %>%
addCircles(
data = filtered_data(),
lng = ~Longitude,
lat = ~Latitude,
radius = 30,
color = "black",
fillOpacity = 1,
weight = 1,
fillColor = filtered_data()$color
)
})
})
shinyApp(ui, server)