Watts-College / paf-513-template

https://watts-college.github.io/paf-513-template/
MIT License
0 stars 0 forks source link

Dashboard Issue - Creating Interactive Map #23

Open lindaalvarez opened 9 months ago

lindaalvarez commented 9 months ago

@JasonSills

Hello, after successfully creating two new tabs with unique information, I am unable to successfully create the map that goes with the information. It is able to run but when I do so, there is no map. I have attached a screenshot of what my browser looks like as well as the code that I am using when attempting to create the map. I would greatly appreciate some guidance. Thank you.


renderLeaflet({

  d10 <- dat %>%
    filter(Weather >= input$weather.of.day[1],
           Lightcondition <= input$condition.light[1], 
           Gender_Drv1 %in% input$driver.1.gender, 
           Unittype_One %in% input$driver.1.pedcy )

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

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

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

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

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

})
Screen Shot 2024-02-25 at 12 52 00 PM
lindaalvarez commented 9 months ago

So I just realized that a map shows up in my lower left corner but does not showcase the information I am trying to relay.

Screen Shot 2024-02-25 at 12 57 02 PM
JasonSills commented 9 months ago

@lindaalvarez

I cannot see the rest of your code, but it looks like you are missing:

Outputs
-------------------------------------

### Traffic Accidents By Day and Time

Go back through the code and you will notice that before each code chunk with renderLeaflet you will find this text. This is telling shiny that the subsequent code chunk is the output and the title is Traffic Accidents By Day and Time. Without this, R thinks that you are still building this on the text before the earlier code chunk:

Traffic Accidents By Day and Time
=====================================  

Inputs {.sidebar}
-------------------------------------

This is for the sidebar and explains why you are getting a map in your sidebar.

lindaalvarez commented 9 months ago

Okay, I will go ahead and keep working on it and let you know if I keep running into issues. Thank you for taking your time to look at this! @JasonSills

lindaalvarez commented 9 months ago
Screen Shot 2024-02-25 at 6 48 26 PM

This is what my site is looking like as of right now. I was able to fix that error but now my code is not showing the appropriate dots and markers @JasonSills

lindaalvarez commented 9 months ago

Additional Conditions {data-orientation=rows} =====================================

Sidebar {.sidebar}

Driver Characteristics


radioButtons(inputId = "Weather", 
             label = h4("Weather on Day of Accident"),
             choices = c("Sunny", 
                         "Cloudy", 
                         "Rain"), 
             selected = "Cloudy")

selectizeInput(inputId = "Lightcondition",
               label = h4("Time of Day"),
                 choices = c("Morning",
                        "Afternoon", 
                        "Evening"), 
            selected = "Morning")

selectInput(inputId = "d1gender", 
            label = h4("Driver 1 Gender"), 
            choices = c("Male",
                        "Female", 
                        "Unknown"), 
            selected = "Male")

selectInput(inputId = "d2gender", 
            label = h4("Driver 2 Gender"), 
            choices = c("Male",
                        "Female", 
                        "Unknown"), 
            selected = "Male")

radioButtons(inputId = "SurfaceCondition", 
            label = h4("Street Condition"), 
            choices = c("No Pot Holes",
                        "Pot Holes", 
                        "Unknown"), 
            selected = "No Pot Holes")

radioButtons(inputId = "d1pedcy", 
             label = h4("Driver 1 Transportation"),
             choices = c("Driver", 
                         "Pedalcyclist", 
                         "Pedestrian"), 
             selected = "Driver")

radioButtons(inputId = "d2pedcy", 
             label = h4("Driver 2 Transportation"),
             choices = c("Driver", 
                         "Pedalcyclist", 
                         "Pedestrian"), 
             selected = "Driver")

Outputs

Driver Characteristics


renderLeaflet({

  d10 <- dat %>%
    filter(Weather >= input$weather.of.day,
           Lightcondition <= input$condition.light, 
           SurfaceCondition <= input$surface.condition,
           Gender_Drv1 %in% input$driver.1.gender, 
           Unittype_One %in% input$driver.1.pedcy )

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

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

  crash.details <- paste0("Time: ", d10$DateTime, "<br>",
                          "Total Fatalities: ", d10$Totalfatalities, "<br>",
                          "Total Injuries: ", d10$Totalinjuries, "<br>",
                          "Weather", d10$Weather, "<br>",
                          "Light Condition", d10$Lightcondition, "<br>",
                          "SurfaceCondition", d10$SurfaceCondition, "<br>", 
                          "Collision type: ", d10$Collisionmanner)

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

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

})

This is the complete code for that "tab". If you had a second to look into this and let me know where I am going wrong I would greatly appreciate it. Thank you for your time @JasonSills

JasonSills commented 9 months ago

@lindaalvarez

Without seeing the rest of your code, what has me wondering is this portion of the code:


  d10 <- dat %>%
    filter(Weather >= input$weather.of.day,
           Lightcondition <= input$condition.light, 
           SurfaceCondition <= input$surface.condition,
           Gender_Drv1 %in% input$driver.1.gender, 
           Unittype_One %in% input$driver.1.pedcy )

Have you tried substituting <= and >= with %in%?

Also, try reversing your inputs:

d10 <- dat %>%
    filter(Weather %in% input$Weather,
           Lightcondition %in% input$Lightcondition, 
           SurfaceCondition <= input$SurfaceCondition,
           Gender_Drv1 %in% input$driver.1.gender, 
           Unittype_One %in% input$driver.1.pedcy )

Again, I'm not seeing the rest of your code, but your inputs you have the terms used in the section I rewrote. For example, in this section of code:

radioButtons(inputId = "Weather", 
             label = h4("Weather on Day of Accident"),
             choices = c("Sunny", 
                         "Cloudy", 
                         "Rain"), 
             selected = "Cloudy")

inputID = "Weather". This is your input$ for your output section.

lindaalvarez commented 9 months ago

okay I will go ahead and work on that, I will let you know if I am not able to fix this. thank you again!

lindaalvarez commented 9 months ago

I was able to figure out where I went wrong @JasonSills - First I was not using the correct terms for the data set and secondly I was not writing my radio buttons and select input correctly. This is a sample of my correct code!

radioButtons(inputId = "Weather", label = h4("Weather"), choices = c("Clear", "Cloudy", "Rain", "Blowing Sand Soil Dirt", "Fog Smog Smoke"), selected = c("Clear"), inline = TRUE)

selectInput(inputId = "LightCondition", label = h4("Light Conditions"), choices = c("Daylight", "Dark Lighted", "Dusk", "Dawn", "Dark Not Lighted"), selected = "Daylight")