0

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)

enter image description here

deschen
  • 10,012
  • 3
  • 27
  • 50
James
  • 137
  • 7
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Right now you define `test_map` outside of a reactive context and the pop is hard coded to `AQ_df` and doesn't depend on your `input` at all. Are you trying to draw two sets of circles? – MrFlick Sep 25 '21 at 01:05
  • Where does your `AQ_df` come from? – lz100 Sep 27 '21 at 19:44

0 Answers0