Skip to content

Commit

Permalink
Updated with data loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ha292 committed Apr 22, 2015
1 parent 2e246d0 commit 965d3e1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 47 deletions.
54 changes: 46 additions & 8 deletions shiny_propotype/v3/server.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
library(shiny)
library(dplyr)
library(ggplot2)
library(scales)
library(shinydashboard)

# Define server logic required to draw a histogram
getFatalitiesByWekdayData <- function(accidents) {
df <- accidents %>% group_by(date) %>% summarize(fatalities=sum(FATALS))
w <- as.POSIXlt(df$date)$wday
weekdays <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
df$weekday <- factor(weekdays[w+1], levels=weekdays)
df
}

getTimingPlot1 <- function(ds) {
df <- getFatalitiesByWekdayData(ds$accidents)
g <- ggplot(df, aes(x=date, y=fatalities, color=weekday)) +
geom_point() +
theme_bw() +
theme(legend.key = element_blank()) +
theme(legend.title = element_blank()) +
xlab('') +
ylab('Fatalities')
g
}

getFatalitiesByStateAndWeekdayData <- function(accidents) {
df <- accidents %>% group_by(state, wday=as.POSIXlt(date)$wday) %>% summarize(fatalities=sum(FATALS))
weekdays <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
df$weekday <- factor(weekdays[df$wday+1], levels=weekdays)
df
}

getTimingPlot2 <- function(ds) {
df <- getFatalitiesByStateAndWeekdayData(ds$accidents)
g <- ggplot(df, aes(x=state, y=fatalities, color=weekday)) +
geom_point() +
theme_bw() +
theme(legend.key = element_blank()) +
theme(legend.title = element_blank()) +
xlab('') +
ylab('Fatalities')
g
}

shinyServer(function(input, output) {
set.seed(122)
histdata <- rnorm(500)

output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
ds <- loadFatalityDataset(2013, '../../')
output$timingPlot1 <- renderPlot({getTimingPlot1(ds)})
output$timingPlot2 <- renderPlot({getTimingPlot2(ds)})
})


77 changes: 43 additions & 34 deletions shiny_propotype/v3/ui.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
library(shinydashboard)

# Define UI for application that draws a histogram
source('../../util/dataloader2.R')

dashboardPage(
dashboardHeader(
title = "US Roads Fatality Data"),
dashboardSidebar(
sidebarMenu(
menuItem("General summary", tabName="summary_tab", icon=icon("car")),
menuItem("Timing", tabName="timing_tab", icon=icon("line-chart")),
menuItem("Alcohol", tabName="alco_tab", icon=icon("beer")),
menuItem("Children", tabName="children_tab", icon=icon("child")),
menuItem("Bikes", tabName="bike_tab", icon=icon("bicycle")),
Expand All @@ -20,22 +22,32 @@ dashboardPage(

#######################
tabItems(
tabItem(tabName="summary_tab",
# Boxes need to be put in a row (or column)
fluidRow(
column(width=12,
h2("General summary"),
p("by state"),img(src='images/test_plot1.png', height = 527, width = 739),
p("by number of fatalities"),
p("by type of vehicle"),
p("Rollover fatalities"),
p("Fatalities vs Restraint")

)
)
),
tabItem(tabName="summary_tab",
# Boxes need to be put in a row (or column)
fluidRow(
column(width=12,
h2("General summary"),
p("by state"),
p("by number of fatalities"),
p("by type of vehicle"),
p("Rollover fatalities"),
p("Fatalities vs Restraint")
)
)
),


#######################
tabItem(tabName="timing_tab",
# Boxes need to be put in a row (or column)
fluidRow(
column(width=12),
h3('All 2013 Fatalities by weekday'),
plotOutput("timingPlot1"),
h3('All 2013 Fatalities by weekday and State'),
plotOutput("timingPlot2")
)
),

#######################
tabItem(tabName="alco_tab",
# Boxes need to be put in a row (or column)
Expand Down Expand Up @@ -97,25 +109,22 @@ tabItems(
p(""))
)
),



#######################
tabItem(tabName="trends_tab",
# Boxes need to be put in a row (or column)
fluidRow(
column(width=12,
h2("Trends"),
p("Seasonality by state"),
p("Average seasonality"),
p("Average seasonality"),
p("By total number of fatalities (seasonal trends)"),
p("Fatalities vs weekday"),
p("Fatalities by type of the vehicle"),
p(""),
p(""))
)
),
tabItem(tabName="trends_tab",
# Boxes need to be put in a row (or column)
fluidRow(
column(width=12,
h2("Trends"),
p("Seasonality by state"),
p("Average seasonality"),
p("Average seasonality"),
p("By total number of fatalities (seasonal trends)"),
p("Fatalities vs weekday"),
p("Fatalities by type of the vehicle"),
p(""),
p(""))
)
),



Expand Down
19 changes: 14 additions & 5 deletions util/dataloader2.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
loadFatalityDataset <- function(year) {
prepareAccidents <- function(a) {
a$date <- as.Date(sprintf('%4s-%02s-%02s', a$YEAR, a$Month, a$Calendar_DAY))
a$state <- a$State.Abbreviation
a
}

loadFatalityDataset <- function(year, rootDir='') {
ds <- c()
zipFileName <- paste('data/NHTSA_Fatality-', year, '.zip', sep='')
zipFileName <- paste(rootDir, 'data/NHTSA_Fatality-', year, '.zip', sep='')
ds$accidents <- read.csv(unz(zipFileName, 'Accident-2013.csv'))
ds$accidents <- prepareAccidents(ds$accidents)

ds$persons <- read.csv(unz(zipFileName, 'Person-2013.csv'))

ds$vehicles <- read.csv(unz(zipFileName, 'Vehicle-2013.csv'))
ds$states <- read.csv('data/states.csv')
ds$avm <- read.csv(paste('data/annual-vehicle-miles-', year, '.csv', sep=''))
ds$urbanPct <- read.csv(paste('data/urban-percent-', year, '.csv', sep=''))
ds$states <- read.csv(paste(rootDir, 'data/states.csv', sep=''))
ds$avm <- read.csv(paste(rootDir, 'data/annual-vehicle-miles-', year, '.csv', sep=''))
ds$urbanPct <- read.csv(paste(rootDir, 'data/urban-percent-', year, '.csv', sep=''))
ds
}

0 comments on commit 965d3e1

Please sign in to comment.