-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
343 lines (290 loc) · 14.9 KB
/
app.R
File metadata and controls
343 lines (290 loc) · 14.9 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
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
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# for (i in dev.list()[1]:dev.list()[length(dev.list())]) {
# dev.off()
# } # code if it shows pdf full
#----------------------------------------------------------------------
#Shiny app gives a high level view for the status of ELectric Vehicles and its associated infrastructure in CA
#Number of datasets = 3
#Number of Plots = 6
#Number of Menu items = 4
#Number of tabs = 6
#Number of inputs = 5
#Info boxes = 3
#Data table = 1
#Download button = 1
## Load libraries
library(shiny)
library(shinydashboard)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(dashboardthemes)
# Load and clean data ----------------------------------------------
# dataset 1
ev.sales <- read.csv("new ZEV sales.csv")
ev.population <- read.csv("vehicle_population.csv")
#cleaning EV data
ev.chargers <- read.csv("ev_chargers.csv")
#Select data for only CA and EVs
ev.chargers1 <- ev.chargers[ev.chargers$State == "CA" &
ev.chargers$Fuel.Type.Code == "ELEC"
,c("Fuel.Type.Code","City","EV.Level1.EVSE.Num",
"EV.Level2.EVSE.Num","EV.DC.Fast.Count" )]
#groupby City
ev <- ev.chargers1 %>% group_by(City)%>%
summarise(count =n(),
Level_one = sum(EV.Level1.EVSE.Num, na.rm=TRUE),
Level_two = sum(EV.Level2.EVSE.Num, na.rm = TRUE),
DC_Fast = sum(EV.DC.Fast.Count, na.rm =TRUE)) %>%
mutate(total = Level_one +Level_two +DC_Fast)
ev <- ev[order(-ev$total),]
## dataset2
counties = unique(ev.sales$County)
county_wise_sales <- ev.sales %>% group_by(Data.Year, County) %>% summarise( total_sales = sum(Number.of.Vehicles, na.rm = TRUE))
county_wise_sales$Data.Year <- as.factor(county_wise_sales$Data.Year)
## datset3
fuel <- ev.population %>% group_by(Data.Year, Fuel.Type) %>% summarise(vehicles_population = sum(Number.of.Vehicles,na.rm =TRUE))
fuel$Data.Year <- as.factor(fuel$Data.Year)
fuel_type <- unique(fuel$Fuel.Type)
ggplot(data = fuel, aes(x = Data.Year, color = Fuel.Type))+
geom_line(aes(y = vehicles_population, group =1))+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust =1, vjust =1))+
xlab("City in CA") + ylab("Numbers of charging stations") + facet_wrap(.~Fuel.Type, scales = "free")
# Avoid plotly issues ----------------------------------------------
pdf(NULL)
# Application header & title ----------------------------------------------
header <- dashboardHeader(title = "Electric Vehicles (CA)",
# Drop down menu with hard coded values ------------------------------
dropdownMenu(type = "notifications",
notificationItem(text = "Forecasting done!",
icon = icon("users"))
),
dropdownMenu(type = "tasks", badgeStatus = "success",
taskItem(value = 110, color = "green",
"EV lists")
),
dropdownMenu(type = "messages",
messageItem(
from = "Arun",
message = HTML("Let's put on our green SWAG!! <br> Go Green!."),
icon = icon("exclamation-circle"))
)
)
# Dashboard Sidebar ----------------------------------------------
sidebar <- dashboardSidebar(
sidebarMenu(
id = "tabs",
# Menu Items ----------------------------------------------
menuItem("Electric vehicle Sale", icon = icon("car"), tabName = "sale"),
menuItem("Total Vehicle Population", icon = icon("truck"), tabName = "newvehicle"),
menuItem("Electric Vehicle Chargers", icon = icon("battery-half"), tabName = "EVchargers"),
menuItem("DATA TableEVC", icon = icon("table"), tabName = "tableevc"), #badgeLabel = "new", badgeColor = "green"),
br(), # break for better visibility
#INputs: county selected
selectInput("county",
"Select County For Total Vehicle Population:",
choices = counties,
multiple = FALSE,
#selectize = TRUE,
selected = "Los Angeles"),
#input: Fuel type wise vehicles sold
selectInput("fuel",
"Select Fuel Type for sold electric vehicles:",
choices = fuel_type,
multiple = FALSE,
#selectize = TRUE,
selected = "Diesel"),
# top x Selection ----------------------------------------------
sliderInput("topSelect",
"How many top cities-number of chargers?",
min = 5,
max = 25,
value = 5,
step = 1),
# Inputs: select level of chargers to plot ----------------------------------------------
selectInput("chargeLevel",
"Charger Level Trend:",
choices = c("Level_one", "Level_two", "DC_Fast", "total"),
multiple = FALSE,
#selectize = TRUE,
selected = "total"),
br(),
br(),
br(),
# Write sampled data as csv ------------------------------------------
actionButton(inputId = "write_csv",
label = "Write CSV")
)
)
# Dashboard body ----------------------------------------------
body <- dashboardBody(shinyDashboardThemes(theme = "blue_gradient"), # add blue gradient theme
# Input and Value Boxes ----------------------------------------------
fluidRow(
infoBoxOutput("EVcars"),
infoBoxOutput("Totalchargers"),
valueBoxOutput("Totalcars")
),
tabItems(
# Plot page ----------------------------------------------
tabItem("EVchargers",
# Plot ----------------------------------------------
fluidRow(
tabBox(title = "Electric Vehicle Charger: Status across CA",
width = 12,
tabPanel("Chargers for top cities", plotlyOutput("plot_charger")),
tabPanel("Inequity within charger distribution", plotlyOutput("plot_char")))
)
),
#uiOutput(outputId = "n"),
# Data Table Page ----------------------------------------------
tabItem("tableevc",
fluidPage(
box(title = "List of level-wise Chargers", DT::dataTableOutput("table_evc"), width = 12))
),
## tab item new vehicle
tabItem("newvehicle",
# Plot ----------------------------------------------
fluidRow(
tabBox(title = "Fueltype wise Total Vehicle Population",
width = 12,
tabPanel("Yearly trend for vehicles", plotlyOutput("plot_fuel")),
tabPanel("Fuel wise trend", plotlyOutput("plot_facet")))
)
),
## tab items total sales
tabItem("sale",
# Plot ----------------------------------------------
fluidRow(
tabBox(title = "Total Electric Vehicle Sales",
width = 12,
tabPanel("Sales trend for a County", plotlyOutput("sales_county")),
#tabPanel("A2", plotlyOutput("plot_char"))),
tabPanel("Fuel Type wise distribution", plotlyOutput("pie")))
)
)
)
)
#create ui
ui <- dashboardPage( header, sidebar, body)
# Define server function required to create plots and value boxes -----
server <- function(input, output) {
# Reactive data function -------------------------------------------
#select by charger type
evInput <- reactive({
ev1 <- select(ev, c("City",input$chargeLevel))
# Return dataframe ----------------------------------------------
return(ev1)
})
# Reactive data function -------------------------------------------
# select by county
salesInput <- reactive({
ev1 <- filter(county_wise_sales, County == input$county)
# Return dataframe ----------------------------------------------
return(ev1)
})
# Reactive data function -------------------------------------------
#select by fuel type
fuelInput <- reactive({
ev1 <- filter(fuel, Fuel.Type == input$fuel)
# Return dataframe ----------------------------------------------
return(ev1)
})
# A plot showing the chargers with city for top selected -----------------------------
output$plot_charger <- renderPlotly({
dat <- evInput() #call data
# Generate Plot ----------------------------------------------
ggplot(data = dat[1:input$topSelect[1],], aes_string(x = "City", y = input$chargeLevel))+
geom_line(aes(group =1), color = "blue")+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust =1, vjust =1))+
xlab("City in CA") + ylab("Numbers of charging stations")
})
# A plot showing the sales for county -----------------------------
output$sales_county <- renderPlotly({
dat <- salesInput()
# Generate Plot ----------------------------------------------
ggplot(data = dat, aes_string(x = "Data.Year", y = "total_sales"))+
geom_line(aes(group =1), color = "blue")+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust =1, vjust =1))+
xlab("Year") + ylab("Numbers of Electric Vehicles")+
ggtitle(paste("Year wise sales for", input$county))
})
## A plot showing the fuel wise distribution -----------------------------
output$pie <- renderPlotly({
type.sales <- ev.sales %>% group_by(Fuel.Type) %>% summarise( total_sales = sum(Number.of.Vehicles, na.rm = TRUE))
ggplot(type.sales, aes(x="Fuel.Type", y=total_sales, fill=Fuel.Type)) +
geom_bar(stat="identity", position = "dodge", color = "white") + ggtitle("Vehicle sales by Fuel type")
})
# A plot showing charging station -----------------------------------
output$plot_char <- renderPlotly({
# Generate Plot ----------------------------------------------
ggplot(data = ev, aes(x = City))+
geom_line(aes(y = total, group =1), color = "blue")+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust =1, vjust =1))+
xlab("City in CA") + ylab("Numbers of charging stations")+theme(axis.text.x=element_blank())+
ggtitle("Plot showing uneven distribution across CA")
})
# A plot showing cnumber of vehicles -----------------------------------
output$plot_fuel <- renderPlotly({
dat <- fuelInput()
# Generate Plot ----------------------------------------------
ggplot(data = dat, aes(x = Data.Year, color = Fuel.Type))+
geom_line(aes(y = vehicles_population, group =1))+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust =1, vjust =1))+
xlab("Year") + ylab(paste("Number of vehicles:", input$fuel)) +
ggtitle(paste("Sales trend for", input$fuel))
})
# A plot showing population of vehicles -----------------------------------
output$plot_facet <- renderPlotly({
dat <- fuel
# Generate Plot ----------------------------------------------
ggplot(data = dat, aes(x = Data.Year, color = Fuel.Type))+
geom_line(aes(y = vehicles_population, group =1))+
theme_bw()+
#theme(axis.text.x = element_text(angle = 60, hjust =1, vjust =1))+
xlab("City in CA") + ylab("Numbers of charging stations")+facet_wrap(.~Fuel.Type, scales ="free")+theme(axis.text.x=element_blank())
})
# Data table of chargers ----------------------------------------------
output$table_evc <- DT::renderDataTable({
ev
})
# charger level info box ----------------------------------------------
output$EVcars <- renderInfoBox({
sw <- evInput()
num <- round(sum(sw[,input$chargeLevel], na.rm = T), 2)
infoBox("Chargers", value = num, subtitle = paste("Charger Type:", input$chargeLevel), icon = icon("battery-half"), color = "purple")
})
# Sales per county level info box ----------------------------------------------
output$Totalcars <- renderInfoBox({
sw <- salesInput()
num <- sum(sw$total_sales , na.rm = T)
infoBox("EV vehicles", value = num, subtitle = paste("County:", input$county), icon = icon("car"), color = "purple")
})
# vehicle population for fuel type value box ----------------------------------------------
output$Totalchargers<- renderValueBox({
sw <- fuelInput()
num <- sum(sw$vehicles_population, na.rm = T)
valueBox(subtitle = paste("Vehicles by Fuel Type in CA:", input$fuel), value = num, icon = icon("truck"))
})
# Write sampled data as csv ---------------------------------------
observeEvent(eventExpr = input$write_csv,
handlerExpr = {
filename <- paste0("Chargercounties", str_replace_all(Sys.time(), ":|\ ", "_"), ".csv")
write.csv(movies_sample(), file = filename, row.names = FALSE)
}
)
}
# Run the application ----------------------------------------------
shinyApp(ui = ui, server = server)