Watts-College / paf-513-template

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

Final Dashboard - Age, Gender, & Method Tab - Rate of Harm Box - Error with Unknown Gender #21

Open bpiontek opened 6 months ago

bpiontek commented 6 months ago

@JasonSills, As I was working on one of my new tabs for the dashboard project, I realized that I was getting an error that read "Error: is.character(color) && length(color) == 1 is not TRUE" in response to one of my calculations.

While attempting to trouble-shoot this, I determined that the root cause was code that I had borrowed from the provided "Age, Gender, & Method" tab, as the same error occurs in the "Rate of Harm" box there when I run the blank template (cpp-526-DashboardFull-LastName.rmd) and set the gender of either driver to "Unknown." In addition, the number of crashes, total injuries, and total fatalities all appear as 0 and the output map appears blank when I run the template and set the gender of either driver to "Unknown," although I can see from reviewing the raw data in RStudio that this should not be the case.

Can you help me figure out how to correct this issue?

bpiontek commented 6 months ago

And I think that the "Number of Crashes," "Total Injuries," "Total Fatalities," and "Rate of Harm" boxes at the top of the template "Comparisons" tab might be pulling from inputs on the "Age, Gender, & Method" tab, although I'm not completely certain about that - could just be my computer lagging.

JasonSills commented 6 months ago

Hi @bpiontek,

I suspect this has to do with NULL values. Once we start filtering by age, etc, our numbers become very small and division by 0 is possible.

You likely have a line of code in your R chunk that reads:

rate.of.harm <- round(length(which(d2$harm == "Harm")) / count(d2), 3)

Let's change that to:

rate.of.harm <- ifelse( nrow(d2) == 0, 0, mean( d2$harm == "Harm" )) %>% round(3)

Let me know how that works for you.

bpiontek commented 6 months ago

@JasonSills Thank you! That fixed it! I also figured out the issue with the boxes across the top of the "Comparisons" tab calling back to the inputs from the "Age, Gender, & Method" tab. It looks like the template borrowed the code from the "AG&M" tab for the "Comparisons" tab and didn't update the inputs. So, where the code says:

d2 <- dat %>% filter(Age_Drv1 >= input$d1age[1] & Age_Drv1 <= input$d1age[2], Age_Drv2 >= input$d2age[1] & Age_Drv2 <= input$d2age[2], Gender_Drv1 %in% input$d1gender, Gender_Drv2 %in% input$d2gender, Unittype_One %in% input$d1pedcy, Unittype_Two %in% input$d2pedcy)

"d1age" should be replaced by "driver.1.age," "d1gender" should be replaced by "driver.1.gender," and so forth. Maybe that was an Easter egg left there for us to find as we work through the project though.