Commit c91a0908 by Sanjay Pant

Added files for Shiny Dashboard

parents
#importing following packages
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
#Packages can be installed using the code below:
#install.packages('leaflet')
#importing data file 'crime.csv'
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#view first few rows
head(df)
#view number of rows and columns
dim(df)
#view data types
str(df)
#Preprocessing
df$YEAR <- as.factor(df$YEAR)
df$HOUR <- as.factor(df$HOUR)
df$MONTH <- as.factor(df$MONTH)
df$OCCURRED_ON_DATE <- as.POSIXct(df$OCCURRED_ON_DATE, format = "%Y-%m-%d %H:%M:%OS")
str(df)
#Shiny App
#importing shiny library
library(shiny)
ui <- basicPage("My first Shiny App")
server <- function(input, output){}
shinyApp(ui = ui, server = server)
#Building a simple Shiny App with an input and an output plot
#Creating a UI object with a fluidPage layout
ui <- fluidPage(
#setting radio buttons input in the UI
radioButtons("year", "Select Year: ",
c("2015" = "2015",
"2016" = "2016",
"2017" = "2017",
"2018" = "2018",
"2019" = "2019")),
#setting an output in form of plot
plotOutput("barplot")
)
#Creating the server object
server <- function(input, output){
#defining the barplot introduced in the UI
output$barplot <- renderPlot({
#filtering data with respect to input from radio buttons using 'year' ID
df_fil <- filter(df, YEAR == input$year)
#defining the barplot using ggplot2
ggplot(data = df_fil, aes(x = df_fil$HOUR)) + geom_bar() + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
}
shinyApp(ui = ui, server = server)
#Building a simple Shiny Dashboard
library(shinydashboard)
#Defining the body of the dashboard
body <- dashboardBody(
#The body consists of a row-based layout with a single fluid row
fluidRow(
#Defining the output within a box
box(plotOutput("barplot"))
)
)
#Defining the header object
header <- dashboardHeader(title = "Boston Crimes Dataset")
#Defining the sidebard object
sidebar <- dashboardSidebar(
radioButtons("year", "Select Year: ",
c("2015" = "2015",
"2016" = "2016",
"2017" = "2017",
"2018" = "2018",
"2019" = "2019"))
)
#Defining our ui object with a dashboardPage template
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$barplot <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
ggplot(data = df_fil, aes(x = df_fil$HOUR)) + geom_bar() + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
}
shinyApp(ui, server)
body <- dashboardBody(
fluidRow(
box(title = "Crime Frequency", solidHeader = TRUE
,collapsible = TRUE, plotOutput("barplot")),
#Adding another box to our fluidRow
box(title = "Map View", solidHeader = TRUE
,collapsible = TRUE, leafletOutput("mapview"))
)
)
header <- dashboardHeader(title = "Boston Crimes Dataset")
sidebar <- dashboardSidebar(
radioButtons("year", "Select Year: ",
c("2015" = "2015",
"2016" = "2016",
"2017" = "2017",
"2018" = "2018",
"2019" = "2019")),
#Adding a dropdown menu within the sidebar
selectInput("offense",
"Offense Description:",
c("All",
unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$barplot <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
ggplot(data = df_fil, aes(x = df_fil$HOUR)) + geom_bar() + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining our 'mapview' plot declared in the UI above
output$mapview <- renderLeaflet({
df_fil_map <- filter(df, OFFENSE_DESCRIPTION == input$offense)
dm <- data.frame(latitude = df_fil_map$Lat[1:500], longitude = df_fil_map$Long[1:500])
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
shinyApp(ui = ui, server = server)
row <- fluidRow(
#tabBox layout with multiple tab panels. Each panel has a plot Output
tabBox(
title = "Crime Frequency",
id = "tabset1", height = "250px",
tabPanel("Per Month", plotOutput("permonth")),
tabPanel("Per Weekday", plotOutput("perday")),
tabPanel("Per Hour", plotOutput("perhour"))
)
,box(title = "Map View", solidHeader = TRUE
,collapsible = TRUE, leafletOutput("mymap"))
)
#Adding components to body:
body <- dashboardBody(row)
header <- dashboardHeader(title = "Boston Crime Dataset")
sidebar <- dashboardSidebar(
radioButtons("year", "Select Year: ",
c("2015" = "2015",
"2016" = "2016",
"2017" = "2017",
"2018" = "2018",
"2019" = "2019")),
selectInput("offense",
"Offense Description:",
c("All",
unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))
)
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating the plotOutput content
output$permonth <- renderPlot({
df1 <- filter(df, YEAR == input$year)
ggplot(data = df1,aes(x=df1$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
df1 <- filter(df, YEAR == input$year)
ggplot(data = df1,aes(x=df1$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
df1 <- filter(df, YEAR == input$year)
ggplot(data = df1,aes(x=df1$HOUR)) + geom_bar(fill="purple") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
output$mymap <- renderLeaflet({
df_fil_off <- filter(df, OFFENSE_DESCRIPTION == input$offense)
dm <- data.frame(latitude = df_fil_off$Lat[1:500], longitude = df_fil_off$Long[1:500])
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#Preprocessing
df$YEAR <- as.factor(df$YEAR)
df$HOUR <- as.factor(df$HOUR)
df$MONTH <- as.factor(df$MONTH)
df$OCCURRED_ON_DATE <- as.POSIXct(df$OCCURRED_ON_DATE, format = "%Y-%m-%d %H:%M:%OS")
row1 <- fluidRow(
tabBox(
title = "Crime Frequency",
id = "tabset1", height = "250px",
tabPanel("Per Month", plotOutput("permonth")),
tabPanel("Per Weekday", plotOutput("perday")),
tabPanel("Per Hour", plotOutput("perhour"))
)
,box(title = "Map View", solidHeader = TRUE
,collapsible = TRUE, leafletOutput("mymap"))
)
#Creating another fluid row
row2 <- fluidRow(
box(title = "Stacked Plot", solidHeader = TRUE
,collapsible = TRUE, plotOutput("stacked"))
,box(title = "Timeseries Plot", solidHeader = TRUE
,collapsible = TRUE, plotOutput("timeseries"))
)
#Adding components to body:
body <- dashboardBody(row1, row2)
## Project Boston Crimes: UI - Header & Sidebars
header <- dashboardHeader(title = "Boston Crime Dataset")
sidebar <- dashboardSidebar(
radioButtons("year", "Select Year: ",
c("2015" = "2015",
"2016" = "2016",
"2017" = "2017",
"2018" = "2018",
"2019" = "2019")),
selectInput("offense",
"Offense Description:",
c(unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))
)
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating the plotOutput content
output$permonth <- renderPlot({
df1 <- filter(df, YEAR == input$year)
ggplot(data = df1,aes(x=df1$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
df1 <- filter(df, YEAR == input$year)
ggplot(data = df1,aes(x=df1$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
df1 <- filter(df, YEAR == input$year)
ggplot(data = df1,aes(x=df1$HOUR)) + geom_bar(fill="purple") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
df_fil_off <- filter(df, OFFENSE_DESCRIPTION == input$offense)
ggplot(data = df_fil_off, aes(x = df_fil_off$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="green")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
df %>% filter(STREET %in% (df %>% count(STREET) %>% arrange(-n) %>% head(10) %>% pull(STREET)),
OFFENSE_DESCRIPTION %in% (df %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(STREET, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")+
scale_fill_ordinal()+
coord_flip()
})
output$mymap <- renderLeaflet({
df_fil_off <- filter(df, OFFENSE_DESCRIPTION == input$offense)
dm <- data.frame(latitude = df_fil_off$Lat[1:500], longitude = df_fil_off$Long[1:500])
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment