0
#Date: "8/1/2021"
#Title:"Project 5"

library(tidyverse)
library(shiny)
library(DT)
library(highcharter)
library(rvest)


#Refer to data: https://github.com/washingtonpost/data-police-shootings/releases/download/v0.1/fatal-police-shootings-data.csv 
"Create a shiny app that allows the user to filter data for each year selected. It is not a good idea to download the data. Instead, read the date remotely.
For the filtered data,
(a) Display all cases with the DT package.
(b) Create a map with the leaflet or highcharter package showing each person killed by the police in 2020. Display the relevant information on the map for each case. (Chapter4 Notes)
(c) Plot the distribution of the variable race using a bar graph.
(d) Plot the distribution of the variable gender.
(e) Plot the distribution of the variable threat_level. 
(f) Plot the distribution of the variable flee.
(g) Plot the distribution of the variable age (with geom_histogram() or geom_density() in ggplot2).
(h) Plot the distribution of the variable armed. 
(i) Plot the top 10 cities that have most victims.
Write a summary under each plot in part (c) through (i). You should have 7 summaries"


url="https://github.com/washingtonpost/data-police-shootings/releases/download/v0.1/fatal-police-shootings-data.csv"
page=read.csv(url)
data<-data.frame(page)%>% mutate(Year=substr(date,1,4))  

ui <- fluidPage(
    
    titlePanel("Police Activity"),
    
    sidebarLayout(
        sidebarPanel(
            selectInput("Year", "Select Year",choices=2015:2021)
        ),
        
        mainPanel(
            tabsetPanel(
                tabPanel("DT",DTOutput("dt")),
                tabPanel("Map",highchartOutput("map")),
                tabPanel("Race",plotOutput("race")),
                tabPanel("Gender",plotOutput("gender")),
                tabPanel("Threat",plotOutput("threat")),
                tabPanel("Flee",plotOutput("flee")),
                tabPanel("Age",plotOutput("age")),
                tabPanel("Armed",plotOutput("armed")),
                tabPanel("City",plotOutput("city"))
            
            )
        )
    )
)
server <- function(input, output) {
    D<-reactive({
        filter(data,Year==input$Year)
    })
    
    output$dt<-renderDT(
        D()
          
    )
    
    output$map<-renderHighchart({
        
        #A).
        
        myformat1<-'<table style="background-color:#00FF00">
        <tr>
         <th>state </th>
         <th>city </th>
         <th>name </th>
         <th>manner_of_death </th>
         <th>armed </th>
         <th>age </th>
         <th>flee </th>
         <th>body_camera </th>
         
        </tr>
        <tr>
         <td>{point.state}</td>
         <td>{point.city}</td>
         <td>{point.name}</td>
         <td>{point.manner_of_death}</td>
         <td>{point.armed}</td>
         <td>{point.age}</td>
         <td>{point.flee}</td>
         <td>{point.body_camera}</td>
         
         </tr>
        </tables>'
         
        data("usgeojson")
         highchart() %>% 
            hc_add_series_map(usgeojson,D(),value = "name",joinBy = c("name","state"),dataLabels=list(enabled=TRUE))%>%
            hc_tooltip(useHTML=TRUE,headerFormat="",pointFormat=myformat1) %>%
            hc_colorAxis(stops=color_stops)%>%
            hc_mapNavigation(enabled=TRUE)
        
    }
    )
    
    output$race<-renderPlot({
         ggplot(D(),aes(race))+
             geom_bar()+
            ggtitle("Race of Victims")
     }   
        
    )   
    output$gender<-renderPlot({
        ggplot(D(),aes(gender))+
            geom_bar()+
            ggtitle("Gender of Victims")
    }
    )
    output$threat<-renderPlot({
        ggplot(D(),aes(threat_level))+
            geom_bar()+
            ggtitle("Threat Level Reported")
    }
    )
    output$flee<-renderPlot({
        ggplot(D(),aes(flee))+
            geom_bar()+
            ggtitle("Did the victim flee?")
     }
    )
    output$age<-renderPlot({
        ggplot(D(),aes(age))+
            geom_density()+
            ggtitle("Age of victims")
    }
        
    )
    output$armed<-renderPlot({
        ggplot(D(),aes(armed))+
            geom_bar()+
            ggtitle("Was the victim armed?")
    }
        
    )
    
    output$city<-renderPlot({
        top.10<-top_n(D(),10,D()$id)
        ggplot(top.10,aes(city))+
            geom_bar()+
            ggtitle("Top 10 cities with most frequent killings")
    }
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

I'm attempting to create a highchart map (A) that displays the selected data when an individual hovers over a state with their mouse. I believe I have all of the components, but for some reason the interactive highchart map won't render (In the app or on the console). Am I missing something or referencing something incorrectly?

Jsnapp01
  • 23
  • 3
  • What is `color_stops`? Could not find it in your code. BTW: It would be easier to help you if you remove all the unnecessary code from your example. See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – stefan Aug 06 '21 at 14:02

0 Answers0