Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The packages need for shiny app
- library(shiny)
- library(lubridate)
- library(tidyverse)
- library(readr)
- ###The code needed for analysing the data frame of Ohio Analysis###
- Ohio_data <- read_rds("data/statewide.rds")
- Ohio_data <- filter(Ohio_data, !is.na(county_name), !is.na(subject_race))
- Ohio_data <- Ohio_data %>%
- mutate(
- citation_issued = NA
- )
- Ohio_data <-Ohio_data%>%
- mutate(year = year(date))
- race_Ohio_data <- Ohio_data %>%
- group_by(subject_race) %>%
- summarise(
- arrest_rate = mean(arrest_made, na.rm = TRUE),
- search_rate = mean(search_conducted, na.rm = TRUE),
- warning_rate = mean(warning_issued, na.rm = TRUE)
- )
- ###The code needed for craeting the data frame for Spatical Analysis (Put this on the top,
- ###Because member say doesn't work put it after loading the package
- Ohio_data$county_name <- gsub("\\s*\\w*$", "", Ohio_data$county_name)
- Ohio_data$county_name <- toupper(Ohio_data$county_name)
- race_rates <- Ohio_data %>%
- group_by(county_name, subject_race) %>%
- summarise(
- arrest_rate = mean(arrest_made, na.rm = TRUE),
- warning_rate = mean(warning_issued, na.rm = TRUE),
- search_rate = mean(search_conducted, na.rm = TRUE)
- )
- #For generating the colopleth maps. helpers.R uses the maps and mapproj packages in R
- library(maps)
- library(mapproj)
- # helpers.R is an R script that can help you make choropleth maps
- library(plyr)
- library(dplyr)
- library(ggplot2)
- library(rgeos)
- library(rgdal)
- library(maptools)
- library(ggrepel)
- library(scales)
- ###THe code need for Spatial Analysis(generating State maps)###
- oh_shp <- readOGR("data/REFER_COUNTY.shp")
- oh_shp <- fortify(oh_shp, region = "COUNTY") #shape file for map
- #The labels used in ggplot
- county_labels <- ddply(oh_shp, .(id), summarize, clat = mean(lat), clong = mean(long))
- #The data frame for producing balck and white maps
- race_rates_bw <- filter(race_rates, subject_race == "black" | subject_race == "white")
- race_rates_b <- filter(race_rates_bw, subject_race == "black")
- race_rates_w <- filter(race_rates_bw, subject_race == "white")
- rm(race_rates_bw)
- race_rates_bw <- merge(race_rates_b, race_rates_w, by = "county_name")
- race_rates_bw$arrest_rate_Delta <- race_rates_bw$arrest_rate.x - race_rates_bw$arrest_rate.y
- race_rates_bw$search_rate_Delta <- race_rates_bw$search_rate.x - race_rates_bw$search_rate.y
- race_rates_bw$warning_rate_Delta <- race_rates_bw$warning_rate.x - race_rates_bw$warning_rate.y
- shinyApp(
- # Define UI for application that draws a histogram
- ui = tagList(
- shinythemes::themeSelector(),
- h2(textOutput("currentTime")),
- navbarPage(
- # theme = "cerulean", # <--- To use a theme, uncomment this
- "Group3: Policing stop analysis",
- ##First module 01: "Columbus Analysis"
- tabPanel("Columbus analysis",
- sidebarPanel(
- selectInput("picture1", "Choose a pictire:",
- choices = c("Stop numbers each year in Columbus",
- "Stop counts/population in Columbus",
- "Outcome types and rates in Columbus",
- "White VS Black"),
- selected = "Stop numbers each year in Columbus" #default value
- ),
- # choices = c("1",
- # "2",
- # "3",
- # "4")),
- h3("Modules was designed for the result of Wang Zhaohong's analysis."),
- h3("Racial discrimination in Columbus region."),
- actionButton("update", "Update View")
- ),#For sidebarPanel
- mainPanel(
- tabsetPanel(id = "module01",
- tabPanel("Stop numbers each year in Columbus",
- # value = "1",
- value = "Stop numbers each year in Columbus",
- img(src = "01.png", height = 720, width = 1280),
- hr(),
- h4("Discover: In this graph, we are compring the stop_count for each race,
- but since the larger proportion of white driver, so the comparison seems compatitive!")
- ),
- tabPanel("Stop counts/population in Columbus",
- # value = "2",
- value = "Stop counts/population in Columbus",
- img(src = "02.PNG", height = 720, width = 1280)
- ),
- tabPanel("Outcome types and rates in Columbus",
- value = "Outcome types and rates in Columbus",
- # value = "3",
- img(src = "03.PNG", height = 720, width = 1280)
- ),
- tabPanel("White VS Black",
- value = "White VS Black",
- # value ="4",
- img(src = "04.PNG", height = 720, width = 1280)
- )#For last tabPanel
- )#For tabsetPanel
- )#For mainPanel
- ),#For tabPanel
- ##Second module 02: "Spatial Analysis"
- tabPanel("Difference in Black and White Analysis",
- sidebarPanel(
- selectInput("picture2", "Choose a pictire:",
- choices = c("arrest_rate", "search_rate", "warning_rate"),
- selected = "arrest_rate"#default value
- ),
- ##TD: Need explanation for spatial analysis(CJ)
- h3("This module is designed for result of Cornelius Johnson!"),
- h3("This result shows the spacial distribution of discrimination across the whole United States."),
- actionButton("update", "Update View")
- ),#For sidebarPanel
- mainPanel(
- tabsetPanel(id = "module02", ##The id must declared as Literal String
- tabPanel("arrest_rate",
- value = "arrest_rate",
- plotOutput(outputId = "plot21", height = "720", width = "1280")
- )
- )#For tabsetPanel
- )#For mainPanel
- ),#For tabPanel
- ##Thrid module 03: "Ohio Analysis"
- tabPanel("Ohio Analysis",
- sidebarPanel(
- "Outcome Type",
- selectInput("picture3", "Choose a type of rate: ",
- choices = c("arrest_rate", "search_rate", "warning_rate"),
- selected = "arrest_rate"
- )
- ##TD: Add description for drop down manu(CJ), don't forget to have a common beforehand
- ),#For sidebarPanel
- mainPanel(
- tabsetPanel(id = "module03",
- tabPanel("Type of rate",
- value = "arrest_rate",
- plotOutput(outputId = "plot31", height = "720", width = "1280")
- )
- # ,
- # tabPanel("search_rate",
- # value = "search_rate",
- # plotOutput(outputId = "plot32", height = "720", width = "1280")
- # ),
- # tabPanel("warning_rate",
- # value = "warning_rate",
- # plotOutput(outputId = "plot33", height = "720", width = "1280")
- # )
- )#For tabsetPanel
- )#For mainPanel
- ),#For tabPanel(Ohio analysis), end
- ##Fourth module 04: "Spatial Analysis Update"
- tabPanel("Spatial Analysis Update",
- sidebarPanel(
- selectInput("picture4", "Choose a type of race:",
- choices = c("asian", "black", "hispanic", "other" ,"white"),
- selected = "asian"#default value
- ),
- ##TD: add an explanation for your spacial analysis, and might what diferent race could do(CJ)
- actionButton("update", "Update View")
- ),#For sidebarPanel
- mainPanel(
- tabsetPanel(id = "module04",
- #Panel 1
- tabPanel("arrest_rate",
- value = "arrest_rate"
- ,plotOutput(outputId = "plot41", height = "720", width = "1280")
- ),
- # Panel 2
- tabPanel("search_rate",
- value = "search_rate"
- ,plotOutput(outputId = "plot42", height = "720", width = "1280")
- ),
- #Panel 3
- tabPanel("warning_rate",
- value = "warning_rate"
- ,plotOutput(outputId = "plot43", height = "720", width = "1280")
- )#End of all tabPanel
- )#For tabsetPanel
- )#For mainPanel
- )#For tabPanel(Spatial Analysis Update), end
- )#For navBarpage
- ),
- server = function(input, output, session) {
- ###First module 01: "Columbus Analaysis"
- observeEvent(input$picture1, {
- updateTabsetPanel(session, "module01",
- selected = input$picture1
- )
- })
- ###Second module 02: "Difference Blakck and white Analysis"
- observeEvent(input$picture2, {
- updateTabsetPanel(session, "module02",
- selected = input$picture2
- )
- })
- #for arrest_rate
- output$plot21 <- renderPlot({
- ##The switch button to change the value of input of y for ggplot
- rate <- switch(input$picture2,
- "arrest_rate" = race_rates_bw$arrest_rate_Delta,
- "search_rate" = race_rates_bw$search_rate_Delta,
- "warning_rate" = race_rates_bw$warning_rate_Delta
- )
- fill <- switch(input$picture2,
- "arrest_rate" = arrest_rate_Delta,
- "search_rate" = search_rate,
- "warning_rate" = warning_rate
- )
- })
- ##Third module 03: "Ohio State Analysis"
- #@input:
- ##plot1 is the output obejct from mainPanel;
- ##'input$picture3' is the selection made from user, arrest_rate, search_rate, warning_rate
- observeEvent(input$picture3, {
- updateTabsetPanel(session, "module03",
- selected = input$picture3
- )
- })
- #for arrest_rate
- output$plot31 <- renderPlot({
- y <- switch(input$picture3,
- "arrest_rate" = race_Ohio_data$arrest_rate,
- "search_rate" = race_Ohio_data$search_rate,
- "warning_rate" = race_Ohio_data$warning_rate
- )
- title <- switch(input$picture3,
- "arrest_rate" = "Arrest Rate",
- "search_rate" = "Search Rate",
- "warning_rate" = "Warning Rate"
- )
- ggplot(race_Ohio_data, aes(x = subject_race, y, fill = subject_race)) +
- geom_bar(stat = "identity", position = position_dodge()) +
- labs(title = c("Racial Disparities in ", title ,"in Ohio"),
- x = "Driver Race",
- y = input$picture3,
- fill = "Driver Race")
- })
- ###"Forth Module: "Spatial Analysis update"###
- observeEvent(input$picture4, {
- updateTabsetPanel(session, "module04",
- selected = input$picture4
- )
- })
- output$plot41 <- renderPlot({
- race <- switch(input$picture4,
- "asian" = "asian/pacific islander",
- "black" = "black",
- "hispanic" = "hispanic",
- "other" = "other/unknown",
- "white" = "white"
- )
- # quantity <- filter(race_rates, subject_race == "hispanic")$arrest_rate
- # ggplot() + geom_map(data = filter(race_rates, subject_race == "hispanic"), aes(map_id = county_name, fill = arrest_rate),
- # map = oh_shp) +
- # expand_limits(
- # x = oh_shp$long -1,
- # y = oh_shp$lat) +
- # scale_fill_gradient2(
- # low = "red",
- # mid = "white",
- # midpoint = median(race_rates$arrest_rate),
- # high = scales::muted("blue"),
- # limits = c(min(race_rates$arrest_rate), max(race_rates$arrest_rate)+.01)) +
- # geom_text_repel( aes(x = clong, y = clat, label = id),
- # data = county_labels,
- # size = 2.25,
- # point.padding = NA) +
- # theme_minimal() +
- # theme(axis.text.x=element_blank(),
- # axis.text.y = element_blank())+
- # labs(title = "Selected Race Arrest Rate Distribution",
- # fill = "Arrest Rate")
- ##Genearting the graph
- # ggplot...
- #Code for testing ggplot
- # ggplot(data = mpg) +
- # geom_point(mapping = aes(x = displ, y = hwy, color = class))
- })
- #Showing current time(Optional)
- output$currentTime <- renderText({
- invalidateLater(1000, session) #1000 milisecond = 1 sec
- paste("The current time is", Sys.time())
- })
- }#End of server
- )#End of shinyApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement