-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
360 lines (332 loc) · 9.13 KB
/
app.R
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Shiny app that displays safety information about cars.
# Uses the API from the National Highway Traffic Safety Administration (NHTSA).
library(curl)
library(jsonlite)
library(shiny)
# Read data from a URL and return NULL on error.
# https://stackoverflow.com/a/12195574/5666087
readURL <- function(url) {
con <- curl::curl(url)
out <- tryCatch(
expr = {
readLines(con = con, warn = FALSE)
},
error = function(cond) {
message(paste("Error getting", url))
message("Here's the original error message:")
message(cond)
return(NULL)
},
finally = {
close(con)
}
)
return(out)
}
# Define UI ----
ui <- {
fluidPage(
titlePanel("How safe is your car? Find out using public data from NHTSA.gov"),
# Sidebar on left and main panel on right ----
sidebarLayout(
# User inputs ----
sidebarPanel(
selectInput(
inputId = "vehicleYear",
label = "Select a model year",
choices = 1995:2023,
selected = 2019
),
selectizeInput(
inputId = "vehicleMake",
label = "Select a make (supports text search)",
choices = NULL,
),
radioButtons(
inputId = "vehicleType",
label = "Select a vehicle type",
choices = c("Please wait"),
),
selectizeInput(
inputId = "vehicleModel",
label = "Select a model",
choices = NULL,
),
selectizeInput(
inputId = "vehicleVariantID",
label = "Select a variant",
choices = NULL,
),
# This is out of 12.
width = 4
),
# Space for data to be rendered ----
mainPanel(fluidRow(column(
12,
fluidRow(textOutput("vehicleInfo")),
fluidRow(
column(4, tableOutput(
"vehicleCrashTable"
)),
column(
8,
fluidRow(
column(6, tableOutput(
"vehicleAssistTable"
)),
column(6, tableOutput(
"vehicleRecallTable"
)),
),
fluidRow(htmlOutput("vehicleImage")),
)
)
)), )
),
hr(),
fluidRow(column(
12,
align = "center",
tags$a(
href = "https://github.com/kaczmarj/car-safety-shiny",
target = "_blank",
rel = "noopener",
"View source",
icon("fab fa-github")
),
"|",
tags$a(
href = "https://github.com/kaczmarj/car-safety-shiny/issues/new",
"Report a problem",
target = "_blank",
rel = "noopener"
),
)),
)
}
# Get vehicle makes ----
# Column names are make_id and make_name.
# TODO: can we run this outside of server and client?
vehicleMakes <- {
res <-
readURL("https://vpic.nhtsa.dot.gov/api/vehicles/GetAllMakes?format=csv")
if (is.null(res)) {
NULL
} else {
read.csv(text = res)
}
}
# We make these objects global for easier debugging.
vehicleModels <- NULL
vehicleTypes <- NULL
vehicleDescriptionsAndIDs <- NULL
vehicleSafety <- NULL
# Define server logic ----
server <- function(input, output, session) {
# See https://shiny.rstudio.com/articles/selectize.html#server-side-selectize
# We set the choices here to improve performance.
updateSelectizeInput(
session = session,
inputId = "vehicleMake",
choices = sort(vehicleMakes$make_name),
selected = "VOLKSWAGEN",
server = TRUE
)
# Fill in the vehicle type options. ----
observe({
make <- input$vehicleMake
# Do not make an API request unless we have selected a make...
if (make %in% vehicleMakes$make_name) {
vehicleTypes <<- {
url <-
sprintf(
"https://vpic.nhtsa.dot.gov/api/vehicles/GetVehicleTypesForMake/%s?format=csv",
make
)
url <- URLencode(url)
res <- readURL(url)
if (is.null(res)) {
NULL
} else {
read.csv(text = res)
}
}
if (is.null(vehicleTypes)) {
return(NULL)
}
vehicleTypeChoices <-
sort(unique(trimws(vehicleTypes$vehicletypename)))
selected <-
if ("Passenger Car" %in% vehicleTypeChoices) {
"Passenger Car"
} else {
NULL
}
updateRadioButtons(
session = session,
inputId = "vehicleType",
# We need to trim whitespace because some names come with whitespace.
choices = vehicleTypeChoices,
selected = selected
)
}
})
# Update choices for vehicle make based on selected vehicle model. ----
observe({
year <- input$vehicleYear
make <- input$vehicleMake
type <- input$vehicleType
# Do not make an API request unless we have selected a make...
if (make %in% vehicleMakes$make_name) {
# Note the <<- operator, so we update the variable in the parent scope.
vehicleModels <<- {
url <-
sprintf(
"https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeYear/make/%s/modelyear/%s/vehicletype/%s?format=csv",
make,
year,
type
)
url <- URLencode(url)
res <- readURL(url)
if (is.null(res)) {
NULL
} else {
read.csv(text = res)
}
}
if (is.null(vehicleModels)) {
return(NULL)
}
updateSelectizeInput(
session = session,
inputId = "vehicleModel",
choices = sort(vehicleModels$model_name)
)
}
})
output$vehicleInfo <- renderText({
allSelected <- all(
input$vehicleYear != "",
input$vehicleMake != "",
input$vehicleModel != "",
!is.null(vehicleModels)
)
if (allSelected) {
# Render text based on the user input ----
sprintf(
"%s %s %s",
input$vehicleYear,
input$vehicleMake,
input$vehicleModel
)
}
})
observe({
year <- input$vehicleYear
make <- input$vehicleMake
model <- input$vehicleModel
url <-
sprintf(
"https://api.nhtsa.gov/SafetyRatings/modelyear/%s/make/%s/model/%s",
year,
make,
model
)
url <- URLencode(url)
allSelected <- all(
year != "", make != "",
model != "", !is.null(vehicleModels)
)
if (allSelected) {
# Reset image.
output$vehicleImage <- renderText("Photo unavailable")
# TODO: should we reset the tables here too?
res <- readURL(url)
if (is.null(res)) {
return(NULL)
}
vehicleDescriptionsAndIDs <<- jsonlite::fromJSON(res)$Results
if (!is.null(vehicleDescriptionsAndIDs) &&
length(vehicleDescriptionsAndIDs) == 0) {
# what do?
} else {
updateSelectizeInput(
session = session,
inputId = "vehicleVariantID",
choices = split(
vehicleDescriptionsAndIDs$VehicleId,
vehicleDescriptionsAndIDs$VehicleDescription
)
)
}
}
})
# Get safety information using the vehicle ID ----
observe({
# A number (actually a string in this case) that is unique to each car.
vehicleId <- input$vehicleVariantID
if (vehicleId == "") {
return()
}
# Get safety information...
url <-
sprintf(
"https://api.nhtsa.gov/SafetyRatings/VehicleId/%s",
vehicleId
)
url <- URLencode(url)
# For debugging: https://api.nhtsa.gov/SafetyRatings/VehicleId/13679
# url above is for 2019 golf
res <- readURL(url)
if (is.null(res)) {
return(NULL)
}
vehicleSafety <<- jsonlite::fromJSON(res)$Results
crashNames <- c(
"OverallRating",
"OverallFrontCrashRating",
"FrontCrashDriversideRating",
"FrontCrashPassengersideRating",
"OverallSideCrashRating",
"SideCrashDriversideRating",
"SideCrashPassengersideRating",
"RolloverRating",
"RolloverRating2",
"RolloverPossibility",
"RolloverPossibility2",
"SidePoleCrashRating"
)
assistNames <-
c(
"NHTSAElectronicStabilityControl",
"NHTSAForwardCollisionWarning",
"NHTSALaneDepartureWarning"
)
recallNames <-
c("ComplaintsCount", "RecallsCount", "InvestigationCount")
output$vehicleCrashTable <- renderTable({
# Transpose the dataframe so column names become row values.
df <- as.data.frame(t(vehicleSafety[, crashNames]))
cbind("Crash test" = rownames(df), Value = df[, 1])
})
output$vehicleAssistTable <- renderTable({
# Transpose the dataframe so column names become row values.
df <- as.data.frame(t(vehicleSafety[, assistNames]))
cbind("Assist system" = rownames(df), Value = df[, 1])
})
output$vehicleRecallTable <- renderTable({
# Transpose the dataframe so column names become row values.
df <- as.data.frame(t(vehicleSafety[, recallNames]))
cbind("Bad news" = rownames(df), Value = df[, 1])
})
output$vehicleImage <- renderText({
c(sprintf(
'<img src="%s" width="500px">',
vehicleSafety$VehiclePicture
))
})
})
}
# Run the application ----
shinyApp(ui = ui, server = server)