-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
229 lines (194 loc) · 7.31 KB
/
app.R
File metadata and controls
229 lines (194 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#
# Name: Ashok Raja
# Project # 2
# Andrew ID: araja1
# Loading libraries
library(shiny)
library(dplyr)
library(plyr)
library(plotly)
library(tibble)
library(shinydashboard)
library(reshape2)
library(shinythemes)
library(shiny)
library(httr)
library(jsonlite)
library(plotly)
library(htmltools)
library(leaflet)
library(rgdal)
ckanSQL <- function(url) {
# Make the Request
r <- RETRY("GET", URLencode(url))
# Extract Content
c <- content(r, "text")
# Basic gsub to make NA's consistent with R
json <- gsub('NaN', 'NA', c, perl = TRUE)
# Create Dataframe
data.frame(jsonlite::fromJSON(json)$result$records)
}
# Unique values for neighborhood Field
ckanUniques <- function(id, field) {
url <- paste0("https://data.wprdc.org/api/3/action/datastore_search_sql?sql=SELECT%20DISTINCT(%22", field, "%22)%20from%20%22", id, "%22")
c(ckanSQL(URLencode(url)))
}
id="e03a89dd-134a-4ee8-a2bd-62c40aeebc6f"
neigh <- ckanUniques(id, "INCIDENTNEIGHBORHOOD")
race <- ckanUniques(id, "RACE")
age<- ckanUniques(id, "AGE")
header <- dashboardHeader(title = "Pittsburgh Arrest Records")
sidebar <- dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("Stats", icon = icon("bar-chart"), tabName = "plot"),
menuItem("Map", icon = icon("map"), tabName = "plot2"),
menuItem("Data", icon = icon("table"), tabName = "table"),
selectInput("neigh_select",
"Neighborhood:",
choices = neigh,
multiple = FALSE,
selectize = TRUE,
selected = 'Manchester'
),
# Selecting the Race
selectInput("race_select",
"Race:",
choices = race,
multiple = FALSE
),
#selecting the age using a slider
sliderInput("age_select",
"Age:",
min = 10,
max = 99,
value = c(10, 99),
step = 1
),
#selecting the gender
checkboxGroupInput("sex_select",
"Sex:",
choices = c('M','F'),
selected=1
)
)
)
body <- dashboardBody(tabItems(
tabItem("plot",
fluidRow(
infoBoxOutput("neighborhoods"),
valueBoxOutput("offences"),
valueBoxOutput("pk")
),
fluidRow(
# Rendering the differnt plots in tabs
tabBox(title = "Plot1",
width = 12,
tabPanel("Crimes by Age", plotlyOutput("plot")),
tabPanel("Crime Timeline", plotlyOutput("plot2"))
)
)
),
tabItem("plot2",
fluidPage(
# Rendering the map
leafletOutput("map")
)
),
tabItem("table",
inputPanel(
# Download Button
downloadButton("downloadData","Download Crime Data")
),
# Rendering the table
fluidPage(
box(title = "Crime Data", DT::dataTableOutput("table"), width=300))
)
)
)
# passing the ui to the dashboard
ui <- dashboardPage(header, sidebar, body)
# Define server logic
server <- function(input, output, session=session)
{
# Caputing the inputs for reactive functions
swInput <- reactive({
url <- paste0("https://data.wprdc.org/api/3/action/datastore_search_sql?sql=SELECT%20*%20from%20%22", id, "%22%20WHERE%20%22INCIDENTNEIGHBORHOOD%22=%20%27",URLencode(input$neigh_select),"%27%20AND%20%22RACE%22%20=%20%27",input$race_select,"%27%20AND%20%22AGE%22%20%3E%27",input$age_select[1],"%27%20AND%20%22AGE%22%20%3C%27",input$age_select[2],"%27")
crime=ckanSQL(url)
if(length(input$sex_select)>0){
crime <- subset(crime, GENDER %in% input$sex_select)
}
return(crime)
})
# Plot for # of crimes by age
output$plot <- renderPlotly({
crime=swInput()
# You want to use fill not colour for bar charts.
ggplotly(ggplot(data=crime,aes(x=AGE,colour=GENDER))+
geom_bar()+
labs(title="Crimes by Age",x="AGE",y="# Crimes",colour="Gender")
)
})
# Plot the crime timeline
output$plot2 <- renderPlotly({
crime=swInput()
ggplotly(ggplot(data=crime,aes(x=as.Date(ARRESTTIME)))+
geom_freqpoly(aes(colour=GENDER))+
labs(title="OffenceTimeline",x="Timeline",y="# of Offences",colour="Gender")
)
})
# Plot the Incident zones and Gender
output$plot3 <- renderPlotly({
crime=swInput()
ggplotly(ggplot(data=crime,aes(x=INCIDENTZONE,colour=GENDER))+
geom_bar()+ coord_flip()+
labs(title="Offenses by Incident Zone",x="Zone",y="# of Offenses",colour="Gender")
)
})
# Data table
# I would have liked to see a subset/select call here to at least remove the x_full_text and _id columns.
output$table <- DT::renderDataTable(crime <-swInput(), options = list(scrollX = TRUE))
# Total CCR info box
output$neighborhoods <- renderInfoBox({
crime= swInput()
num <- length(unique(crime$CCR))
infoBox("# CCR", value = num, icon = icon("balance-scale"), color = "purple")
})
# Total offences per selection
output$offences <- renderValueBox({
crime=swInput()
num <- length(unique(crime$OFFENSES))
valueBox(subtitle = "Unique Offences", value = num, icon = icon("sort-numeric-asc"), color = "green")
})
# Total crimes per selection
output$pk <- renderValueBox({
crime=swInput()
num <- length(crime$PK)
valueBox(subtitle = "# PK", value = num, icon = icon("sort-numeric-asc"), color = "red")
})
# Render Crime Map
output$map <- renderLeaflet({
crime <- swInput()
url <- paste0("https://services1.arcgis.com/YZCmUqbcsUpOKfj7/arcgis/rest/services/PGHWebNeighborhoods/FeatureServer/0/query?where=HOOD+%3D+%27",URLencode(input$neigh_select),"%27&objectIds=&time=&geometry=&geometryType=esriGeometryPolygon&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=&returnGeometry=true&returnCentroid=false&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=geojson&token=")
pitt=readOGR(url)
sp=spTransform(pitt,CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
# Call Data and Build Map
leaflet() %>%
# You really shouldn't have this because you are returning a single neighborhood
setView(lng = -80.0004, lat = 40.4418, zoom = 12)%>%
addProviderTiles(providers$OpenStreetMap.Mapnik,options = providerTileOptions(noWrap = TRUE)) %>%
addPolygons(data=sp,color = "#444444", weight = 1, smoothFactor = 0.5, group="HOOD")%>%
addCircleMarkers(data = crime, lng = ~X, lat = ~Y,radius = 1.5, color = "#E7298A", group="crime", popup = ~OFFENSES)
})
output$downloadData <- downloadHandler(
filename = function() {
paste("download", ".csv", sep = "")
},
content = function(file) {
crime <- swInput()
write.csv(crime, file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server, enableBookmarking = "url")