Watts-College / paf-513-fall-2023

0 stars 0 forks source link

Dashboard #18

Open kdelaro2 opened 1 year ago

kdelaro2 commented 1 year ago

Hi Professor,

I'm having difficulty linking the widget to the leaflet map. Can you clarify what you mean by "all unique IDs must have a corresponding variable in the data" when filtering the output?

I'm trying to show type of collision by time of day, but I'm confused on how to filter collision. Since it's a categorical variable I tried filtering by it's label but the data only ends working for one type of collision and not the rest. The time of day output doesn't seem to be working for me either. Not sure what I'm doing wrong, but any guidance would be greatly appreciated!

Input:

selectInput(inputId = "time.of.day", 
            label = h3("Time of Day"),

            choices = list("Morning Commute",
                           "Evening Commute",
                           "School Pickup",
                           "Work",
                           "Night",
                           "Midnight to Dawn"),
            selected = c("Morning Commute"))

checkboxGroupInput("Collisionmanner", 
                   label = h3("Type of Collision"), 
    choices = list("Rear End",
                   "Left Turn",
                   "Angle (Front to Side)(Other than Left Turn)",
                   "Sideswipe Same Direction",
                   "Single Vehicle",
                   "Other",
                   "Head On",
                   "Unknown",
                   "Sideswipe Opposite Direction",
                   "Rear to Side",
                   "Rear to Rear",
                   "10"),
    selected = c("Rear End",
                 "Left Turn",
                 "Head On"))

Output:

renderLeaflet({

  d2 <- dat %>%
    filter(hour %in% input$time.of.day,
"Rear End" == input$Collisionmanner)

  d2$col.vec <- ifelse(test = d2$nohurt, 
                       yes = "gray20", 
                       no = ifelse(test = d2$inj, 
                                   yes = "steelblue", 
                                   no = "darkorange") ) 

  point.size <- d2$Totalinjuries + d2$Totalfatalities

  crash.details <- paste0("Time: ", d2$DateTime, "<br>",
                          "Total Fatalities: ", d2$Totalfatalities, "<br>",
                          "Total Injuries: ", d2$Totalinjuries, "<br>",
                          "Collision type: ", d2$Collisionmanner)

  tempe <- leaflet( ) %>% 
    addProviderTiles("CartoDB.Positron")  %>%
    setView(lng = -111.9278, 
            lat = 33.39951, 
            zoom = 13)

  addCircles(tempe, 
             lng = d2$Longitude, 
             lat = d2$Latitude,
             fillColor = d2$col.vec, 
             fillOpacity = 0.5, 
             stroke = FALSE, 
             radius = 50 * (1 + 0.33 * point.size),
             popup = crash.details)

})