首页 > 解决方案 > 在 Shiny 中创建交互式 Highchart 地图?

问题描述

#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)

我正在尝试创建一个高图地图 (A),当个人用鼠标悬停在某个州上时显示所选数据。我相信我拥有所有组件,但由于某种原因,交互式高图地图不会呈现(在应用程序或控制台上)。我是否遗漏了某些内容或错误地引用了某些内容?

标签: rhighchartsshiny

解决方案


推荐阅读