Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.13 KB | None | 0 0
  1. #The packages need for shiny app
  2. library(shiny)
  3. library(lubridate)
  4. library(tidyverse)
  5. library(readr)
  6. ###The code needed for analysing the data frame of Ohio Analysis###
  7. Ohio_data <- read_rds("data/statewide.rds")
  8. Ohio_data <- filter(Ohio_data, !is.na(county_name), !is.na(subject_race))
  9. Ohio_data <- Ohio_data %>%
  10. mutate(
  11. citation_issued = NA
  12. )
  13. Ohio_data <-Ohio_data%>%
  14. mutate(year = year(date))
  15.  
  16. race_Ohio_data <- Ohio_data %>%
  17. group_by(subject_race) %>%
  18. summarise(
  19. arrest_rate = mean(arrest_made, na.rm = TRUE),
  20. search_rate = mean(search_conducted, na.rm = TRUE),
  21. warning_rate = mean(warning_issued, na.rm = TRUE)
  22. )
  23.  
  24. ###The code needed for craeting the data frame for Spatical Analysis (Put this on the top,
  25. ###Because member say doesn't work put it after loading the package
  26.  
  27. Ohio_data$county_name <- gsub("\\s*\\w*$", "", Ohio_data$county_name)
  28. Ohio_data$county_name <- toupper(Ohio_data$county_name)
  29.  
  30. race_rates <- Ohio_data %>%
  31. group_by(county_name, subject_race) %>%
  32. summarise(
  33. arrest_rate = mean(arrest_made, na.rm = TRUE),
  34. warning_rate = mean(warning_issued, na.rm = TRUE),
  35. search_rate = mean(search_conducted, na.rm = TRUE)
  36. )
  37.  
  38. #For generating the colopleth maps. helpers.R uses the maps and mapproj packages in R
  39. library(maps)
  40. library(mapproj)
  41. # helpers.R is an R script that can help you make choropleth maps
  42. library(plyr)
  43. library(dplyr)
  44. library(ggplot2)
  45. library(rgeos)
  46. library(rgdal)
  47. library(maptools)
  48. library(ggrepel)
  49. library(scales)
  50.  
  51. ###THe code need for Spatial Analysis(generating State maps)###
  52. oh_shp <- readOGR("data/REFER_COUNTY.shp")
  53. oh_shp <- fortify(oh_shp, region = "COUNTY") #shape file for map
  54. #The labels used in ggplot
  55. county_labels <- ddply(oh_shp, .(id), summarize, clat = mean(lat), clong = mean(long))
  56. #The data frame for producing balck and white maps
  57. race_rates_bw <- filter(race_rates, subject_race == "black" | subject_race == "white")
  58. race_rates_b <- filter(race_rates_bw, subject_race == "black")
  59. race_rates_w <- filter(race_rates_bw, subject_race == "white")
  60. rm(race_rates_bw)
  61. race_rates_bw <- merge(race_rates_b, race_rates_w, by = "county_name")
  62. race_rates_bw$arrest_rate_Delta <- race_rates_bw$arrest_rate.x - race_rates_bw$arrest_rate.y
  63. race_rates_bw$search_rate_Delta <- race_rates_bw$search_rate.x - race_rates_bw$search_rate.y
  64. race_rates_bw$warning_rate_Delta <- race_rates_bw$warning_rate.x - race_rates_bw$warning_rate.y
  65.  
  66.  
  67.  
  68. shinyApp(
  69. # Define UI for application that draws a histogram
  70. ui = tagList(
  71. shinythemes::themeSelector(),
  72. h2(textOutput("currentTime")),
  73.  
  74. navbarPage(
  75. # theme = "cerulean", # <--- To use a theme, uncomment this
  76. "Group3: Policing stop analysis",
  77.  
  78. ##First module 01: "Columbus Analysis"
  79. tabPanel("Columbus analysis",
  80.  
  81. sidebarPanel(
  82. selectInput("picture1", "Choose a pictire:",
  83.  
  84. choices = c("Stop numbers each year in Columbus",
  85. "Stop counts/population in Columbus",
  86. "Outcome types and rates in Columbus",
  87. "White VS Black"),
  88. selected = "Stop numbers each year in Columbus" #default value
  89. ),
  90.  
  91. # choices = c("1",
  92. # "2",
  93. # "3",
  94. # "4")),
  95.  
  96. h3("Modules was designed for the result of Wang Zhaohong's analysis."),
  97. h3("Racial discrimination in Columbus region."),
  98. actionButton("update", "Update View")
  99.  
  100. ),#For sidebarPanel
  101.  
  102. mainPanel(
  103. tabsetPanel(id = "module01",
  104. tabPanel("Stop numbers each year in Columbus",
  105. # value = "1",
  106. value = "Stop numbers each year in Columbus",
  107. img(src = "01.png", height = 720, width = 1280),
  108. hr(),
  109. h4("Discover: In this graph, we are compring the stop_count for each race,
  110. but since the larger proportion of white driver, so the comparison seems compatitive!")
  111. ),
  112. tabPanel("Stop counts/population in Columbus",
  113. # value = "2",
  114. value = "Stop counts/population in Columbus",
  115. img(src = "02.PNG", height = 720, width = 1280)
  116. ),
  117. tabPanel("Outcome types and rates in Columbus",
  118. value = "Outcome types and rates in Columbus",
  119. # value = "3",
  120. img(src = "03.PNG", height = 720, width = 1280)
  121. ),
  122. tabPanel("White VS Black",
  123. value = "White VS Black",
  124. # value ="4",
  125. img(src = "04.PNG", height = 720, width = 1280)
  126. )#For last tabPanel
  127. )#For tabsetPanel
  128. )#For mainPanel
  129. ),#For tabPanel
  130.  
  131.  
  132. ##Second module 02: "Spatial Analysis"
  133. tabPanel("Difference in Black and White Analysis",
  134.  
  135. sidebarPanel(
  136. selectInput("picture2", "Choose a pictire:",
  137. choices = c("arrest_rate", "search_rate", "warning_rate"),
  138. selected = "arrest_rate"#default value
  139. ),
  140. ##TD: Need explanation for spatial analysis(CJ)
  141. h3("This module is designed for result of Cornelius Johnson!"),
  142. h3("This result shows the spacial distribution of discrimination across the whole United States."),
  143. actionButton("update", "Update View")
  144. ),#For sidebarPanel
  145.  
  146. mainPanel(
  147. tabsetPanel(id = "module02", ##The id must declared as Literal String
  148. tabPanel("arrest_rate",
  149. value = "arrest_rate",
  150. plotOutput(outputId = "plot21", height = "720", width = "1280")
  151. )
  152.  
  153. )#For tabsetPanel
  154. )#For mainPanel
  155. ),#For tabPanel
  156.  
  157. ##Thrid module 03: "Ohio Analysis"
  158. tabPanel("Ohio Analysis",
  159.  
  160. sidebarPanel(
  161. "Outcome Type",
  162. selectInput("picture3", "Choose a type of rate: ",
  163. choices = c("arrest_rate", "search_rate", "warning_rate"),
  164. selected = "arrest_rate"
  165. )
  166.  
  167. ##TD: Add description for drop down manu(CJ), don't forget to have a common beforehand
  168.  
  169. ),#For sidebarPanel
  170. mainPanel(
  171. tabsetPanel(id = "module03",
  172. tabPanel("Type of rate",
  173. value = "arrest_rate",
  174. plotOutput(outputId = "plot31", height = "720", width = "1280")
  175. )
  176.  
  177. # ,
  178. # tabPanel("search_rate",
  179. # value = "search_rate",
  180. # plotOutput(outputId = "plot32", height = "720", width = "1280")
  181.  
  182. # ),
  183. # tabPanel("warning_rate",
  184. # value = "warning_rate",
  185. # plotOutput(outputId = "plot33", height = "720", width = "1280")
  186.  
  187. # )
  188.  
  189. )#For tabsetPanel
  190.  
  191. )#For mainPanel
  192. ),#For tabPanel(Ohio analysis), end
  193.  
  194. ##Fourth module 04: "Spatial Analysis Update"
  195. tabPanel("Spatial Analysis Update",
  196.  
  197. sidebarPanel(
  198. selectInput("picture4", "Choose a type of race:",
  199. choices = c("asian", "black", "hispanic", "other" ,"white"),
  200. selected = "asian"#default value
  201. ),
  202. ##TD: add an explanation for your spacial analysis, and might what diferent race could do(CJ)
  203. actionButton("update", "Update View")
  204. ),#For sidebarPanel
  205.  
  206. mainPanel(
  207. tabsetPanel(id = "module04",
  208. #Panel 1
  209. tabPanel("arrest_rate",
  210. value = "arrest_rate"
  211. ,plotOutput(outputId = "plot41", height = "720", width = "1280")
  212. ),
  213. # Panel 2
  214. tabPanel("search_rate",
  215. value = "search_rate"
  216. ,plotOutput(outputId = "plot42", height = "720", width = "1280")
  217. ),
  218. #Panel 3
  219. tabPanel("warning_rate",
  220. value = "warning_rate"
  221. ,plotOutput(outputId = "plot43", height = "720", width = "1280")
  222. )#End of all tabPanel
  223. )#For tabsetPanel
  224. )#For mainPanel
  225.  
  226. )#For tabPanel(Spatial Analysis Update), end
  227. )#For navBarpage
  228. ),
  229.  
  230. server = function(input, output, session) {
  231.  
  232.  
  233.  
  234.  
  235.  
  236. ###First module 01: "Columbus Analaysis"
  237. observeEvent(input$picture1, {
  238. updateTabsetPanel(session, "module01",
  239. selected = input$picture1
  240. )
  241. })
  242.  
  243. ###Second module 02: "Difference Blakck and white Analysis"
  244. observeEvent(input$picture2, {
  245. updateTabsetPanel(session, "module02",
  246. selected = input$picture2
  247. )
  248. })
  249.  
  250. #for arrest_rate
  251. output$plot21 <- renderPlot({
  252.  
  253. ##The switch button to change the value of input of y for ggplot
  254. rate <- switch(input$picture2,
  255. "arrest_rate" = race_rates_bw$arrest_rate_Delta,
  256. "search_rate" = race_rates_bw$search_rate_Delta,
  257. "warning_rate" = race_rates_bw$warning_rate_Delta
  258. )
  259.  
  260. fill <- switch(input$picture2,
  261. "arrest_rate" = arrest_rate_Delta,
  262. "search_rate" = search_rate,
  263. "warning_rate" = warning_rate
  264. )
  265.  
  266. })
  267.  
  268.  
  269. ##Third module 03: "Ohio State Analysis"
  270. #@input:
  271. ##plot1 is the output obejct from mainPanel;
  272. ##'input$picture3' is the selection made from user, arrest_rate, search_rate, warning_rate
  273.  
  274. observeEvent(input$picture3, {
  275. updateTabsetPanel(session, "module03",
  276. selected = input$picture3
  277. )
  278. })
  279.  
  280. #for arrest_rate
  281. output$plot31 <- renderPlot({
  282. y <- switch(input$picture3,
  283. "arrest_rate" = race_Ohio_data$arrest_rate,
  284. "search_rate" = race_Ohio_data$search_rate,
  285. "warning_rate" = race_Ohio_data$warning_rate
  286. )
  287. title <- switch(input$picture3,
  288. "arrest_rate" = "Arrest Rate",
  289. "search_rate" = "Search Rate",
  290. "warning_rate" = "Warning Rate"
  291. )
  292. ggplot(race_Ohio_data, aes(x = subject_race, y, fill = subject_race)) +
  293. geom_bar(stat = "identity", position = position_dodge()) +
  294. labs(title = c("Racial Disparities in ", title ,"in Ohio"),
  295. x = "Driver Race",
  296. y = input$picture3,
  297. fill = "Driver Race")
  298.  
  299. })
  300.  
  301. ###"Forth Module: "Spatial Analysis update"###
  302.  
  303. observeEvent(input$picture4, {
  304. updateTabsetPanel(session, "module04",
  305. selected = input$picture4
  306. )
  307.  
  308. })
  309.  
  310.  
  311.  
  312. output$plot41 <- renderPlot({
  313.  
  314. race <- switch(input$picture4,
  315. "asian" = "asian/pacific islander",
  316. "black" = "black",
  317. "hispanic" = "hispanic",
  318. "other" = "other/unknown",
  319. "white" = "white"
  320. )
  321. # quantity <- filter(race_rates, subject_race == "hispanic")$arrest_rate
  322. # ggplot() + geom_map(data = filter(race_rates, subject_race == "hispanic"), aes(map_id = county_name, fill = arrest_rate),
  323. # map = oh_shp) +
  324. # expand_limits(
  325. # x = oh_shp$long -1,
  326. # y = oh_shp$lat) +
  327. # scale_fill_gradient2(
  328. # low = "red",
  329. # mid = "white",
  330. # midpoint = median(race_rates$arrest_rate),
  331. # high = scales::muted("blue"),
  332. # limits = c(min(race_rates$arrest_rate), max(race_rates$arrest_rate)+.01)) +
  333. # geom_text_repel( aes(x = clong, y = clat, label = id),
  334. # data = county_labels,
  335. # size = 2.25,
  336. # point.padding = NA) +
  337. # theme_minimal() +
  338. # theme(axis.text.x=element_blank(),
  339. # axis.text.y = element_blank())+
  340. # labs(title = "Selected Race Arrest Rate Distribution",
  341. # fill = "Arrest Rate")
  342.  
  343. ##Genearting the graph
  344.  
  345. # ggplot...
  346. #Code for testing ggplot
  347. # ggplot(data = mpg) +
  348. # geom_point(mapping = aes(x = displ, y = hwy, color = class))
  349. })
  350.  
  351. #Showing current time(Optional)
  352. output$currentTime <- renderText({
  353. invalidateLater(1000, session) #1000 milisecond = 1 sec
  354. paste("The current time is", Sys.time())
  355. })
  356. }#End of server
  357. )#End of shinyApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement