DS4PS / cpp-526-spr-2021

Course shell for Foundations of Data Science I
https://ds4ps.org/cpp-526-spr-2021/
MIT License
1 stars 2 forks source link

Dashboard Project - Output not showing data points #16

Open ellihammons21 opened 3 years ago

ellihammons21 commented 3 years ago

Hello,

For one of my tabs, I am attempting to show the number of accidents by driver substance and time of day. For some reason, when I knit the html document the points are not showing on the map and I can't figure out why. I was kind of confused by the variables d1.substance and d2.substance and how to list these in the input, so that might be the issue. Or maybe how I've filtered the variables in the output section? Anywho, I'm stumped. Can anyone point me to the likely culprit of the issue here? Thanks!

Driver Substance by Time of Day =====================================

Inputs {.sidebar}


selectInput(inputId = "d1.substance", 
            label = h4("Driver 1 Substance"), 
            choices = c("Alcohol", 
                        "Drugs", 
                        "No Apparent Influence", 
                        "Alcohol and Drugs"),
            selected = "No Apparent Influence")

selectInput(inputId = "d2.substance", 
            label = h4("Driver 2 Substance"), 
            choices = c("Alcohol", 
                        "Drugs", 
                        "No Apparent Influence", 
                        "Alcohol and Drugs"),
            selected = "No Apparent Influence")

radioButtons(inputId = "time.of.day", 
             label = h4("Time of Day"),
             choices = c("Morning Commute", 
                         "Evening Commute", 
                         "School Pickup", 
                         "Work", 
                         "Night", 
                         "Midnight to Dawn"),
             selected = c("Morning Commute") )

Outputs

Accidents by Driver Substance & Time of Day


renderLeaflet({

  d12 <- dat %>%
    filter(AlcoholUse_Drv1 == input$d1.substance,   
           AlcoholUse_Drv2 == input$d2.substance,
           DrugUse_Drv1 == input$d1.substance,   
           DrugUse_Drv2 == input$d2.substance,
           hour %in% input$time.of.day)

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

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

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

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

  addCircles( tempe, 
              lng=d12$Longitude, 
              lat=d12$Latitude,
              fillColor=d12$col.vec, 
              fillOpacity=0.5, 
              stroke=F, 
              radius=50*(1+0.33*point.size),
              popup=crash.details )
})
jamisoncrawford commented 3 years ago

Hi @ellihammons21 - there are a few things that immediately stand out.

First - check out the input widget for "Time of Day" and possible values versus what you're filtering by in filter().

radioButtons(inputId = "time.of.day", 
             label = h4("Time of Day"),
             choices = c("Morning Commute", 
                         "Evening Commute", 
                         "School Pickup", 
                         "Work", 
                         "Night", 
                         "Midnight to Dawn"),
             selected = c("Morning Commute") )

So your options include "Evening Commute", "School Pickup", etc. However, note how you use these in filter():

dat %>%
    filter(AlcoholUse_Drv1 == input$d1.substance,   
           AlcoholUse_Drv2 == input$d2.substance,
           DrugUse_Drv1 == input$d1.substance,   
           DrugUse_Drv2 == input$d2.substance,
           hour %in% input$time.of.day)                            # This line here.

You're telling R to keep all rows in dat where hour is equal to "Evening Commute", "School Pickup", etc. However, hour is numeric, not categorical:

> head(dat$hour, 12)
 [1]  9 17 19 14 13 17  7 15 20  4 22 13

So you're filtering on the wrong variable, or you aren't using the right widget options for users to select, depending on what you want in your final product. Since these don't match up, it should basically reduce your dataset to 0 rows.

In that same call to filter() I recommend changing the == to %in% in case you have more than one value in your inputs.

Lastly, make sure the exact way the values in your data are spelled, formatted, etc. are also spelled, formatted, etc. the same way in your shiny input widgets. It's easy to add an option in a widget that has an extra space or wrong capitalization, and it can easily break your app!

Let me know if this gets you any further.

ellihammons21 commented 3 years ago

Thanks, @jamisoncrawford! Ok, I reworked some of the code and found a variable that was spelled incorrectly, and now the tab is working as it should. Thanks for the suggestions!

And now for my next issue:

One my final tab, when I run the HTML document, the map is only showing in the top right corner. The picture and code are attached. Any idea what could be causing this?

Thanks again!

Dashboard_error Collision Manner & Driver Age Group =====================================

Inputs {.sidebar}


selectInput(inputId = "Coll.Type", 
            label = h4("Type of Collision"), 
            choices = c("Rear End", 
                        "Rear to Side", 
                        "Left Turn", 
                        "Head On",
                        "Sideswipe Same Direction",
                        "Sideswipe Opposite Direction"),
            selected = "Rear End")

radioButtons(inputId = "age.group.d1", 
             label = h4("Driver 1 Age Group"),
             choices = c("Youth", 
                         "Young Adult", 
                         "Adult", 
                         "Senior"),
             selected = c("Adult") )

radioButtons(inputId = "age.group.d2", 
             label = h4("Driver 2 Age Group"),
             choices = c("Youth", 
                         "Young Adult", 
                         "Adult", 
                         "Senior"),
             selected = c("Adult") )

Outputs

Collision Manner & Driver Age Group


renderLeaflet({

  d13 <- dat %>%
    filter(Collisionmanner %in% input$Coll.Type,   
           age.cat.d1 %in% input$age.group.d1 ,
           age.cat.d2 %in% input$age.group.d2)

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

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

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

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

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