Commit 4feb82da by Sanjay Pant

Updates all files

parent c91a0908
File added
output$perhour <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp, aes(x = df_fil_grp$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
df_fil_grp %>% filter(DISTRICT %in% (df_fil_grp %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (df_fil_grp %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
output$table <- renderDataTable(
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense),
options = list(scrollX = TRUE)
)
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
#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 Police")
#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_economist()
})
}
shinyApp(ui, server)
library(ggthemes)
install.packages('ggthemes')
library(ggthemes)
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_economist()
})
}
shinyApp(ui, server)
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)
?radioButtons
clc
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#remove missing values
df <- na.omit(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")
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered, aes(x = filtered$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered, aes(x = filtered$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered,aes(x = filtered$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered, aes(x = filtered$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered %>% filter(DISTRICT %in% (filtered %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered$Lat, longitude = filtered$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered, aes(x = filtered$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered,aes(x = filtered$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered, aes(x = filtered$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered %>% filter(DISTRICT %in% (filtered %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered$Lat, longitude = filtered$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered() %>% filter(DISTRICT %in% (filtered() %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered() %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
runApp('7. Advance.R')
#importing following packages
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
#importing data file 'crime.csv'
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#view first few rows
head(df)
#remove missing values
df <- na.omit(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)
#importing shiny library
library(shiny)
ui <- basicPage("My first Shiny App")
server <- function(input, output){}
shinyApp(ui = ui, server = server)
#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 Police")
#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 Police")
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$barplot <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp, aes(x = df_fil_grp$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 <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[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({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$HOUR)) + geom_bar(fill="purple") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
output$mymap <- renderLeaflet({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
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=",")
#remove missing values
df <- na.omit(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")
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered() %>% filter(DISTRICT %in% (filtered() %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered() %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
runApp('7. Advance.R')
[]
\ No newline at end of file
{
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards",
"sortOrder" : [
{
"ascending" : true,
"columnIndex" : 2
}
]
}
\ No newline at end of file
{
"activeTab" : 6
}
\ No newline at end of file
{
"left" : {
"panelheight" : 848,
"splitterpos" : 209,
"topwindowstate" : "NORMAL",
"windowheight" : 887
},
"right" : {
"panelheight" : 848,
"splitterpos" : 532,
"topwindowstate" : "NORMAL",
"windowheight" : 887
}
}
\ No newline at end of file
{
"TabSet1" : 0,
"TabSet2" : 0,
"TabZoom" : {
}
}
\ No newline at end of file
{"active_set":"","sets":[]}
\ No newline at end of file
{
"collab_server" : "",
"contents" : "",
"created" : 1571850946504.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "748450125",
"id" : "2DA50A8C",
"lastKnownWriteTime" : 1571926355,
"last_content_update" : 1571926355961,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/2. SimpleShinyApp.R",
"project_path" : "2. SimpleShinyApp.R",
"properties" : {
"cursorPosition" : "36,0",
"scrollLine" : "3"
},
"relative_order" : 2,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
#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)
{
"collab_server" : "",
"contents" : "",
"created" : 1571853125252.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "978112177",
"id" : "7C20CC79",
"lastKnownWriteTime" : 1571936278,
"last_content_update" : 1571936278476,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/6. Final_Dashboard.R",
"project_path" : "6. Final_Dashboard.R",
"properties" : {
"cursorPosition" : "130,0",
"scrollLine" : "97"
},
"relative_order" : 6,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#remove missing values
df <- na.omit(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")
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered() %>% filter(DISTRICT %in% (filtered() %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered() %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
{
"collab_server" : "",
"contents" : "",
"created" : 1571852791181.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "2214789430",
"id" : "96E31DD3",
"lastKnownWriteTime" : 1571920520,
"last_content_update" : 1571920520196,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/5. UpdatedDashboard_withTabs.R",
"project_path" : "5. UpdatedDashboard_withTabs.R",
"properties" : {
"cursorPosition" : "82,0",
"scrollLine" : "49"
},
"relative_order" : 5,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[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({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$HOUR)) + geom_bar(fill="purple") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
output$mymap <- renderLeaflet({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
{
"collab_server" : "",
"contents" : "",
"created" : 1571850698475.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "4141198788",
"id" : "A12A982C",
"lastKnownWriteTime" : 1571926602,
"last_content_update" : 1571926602900,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/1. Preprocessing.R",
"project_path" : "1. Preprocessing.R",
"properties" : {
"cursorPosition" : "50,0",
"scrollLine" : "16"
},
"relative_order" : 1,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
#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)
#remove missing values
df <- na.omit(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)
{
"collab_server" : "",
"contents" : "",
"created" : 1571852670998.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "324470682",
"id" : "A8816B8B",
"lastKnownWriteTime" : 1571920214,
"last_content_update" : 1571920214980,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/4. UpdatedDashboard.R",
"project_path" : "4. UpdatedDashboard.R",
"properties" : {
"cursorPosition" : "64,0",
"scrollLine" : "31"
},
"relative_order" : 4,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
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 Police")
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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$barplot <- renderPlot({
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp, aes(x = df_fil_grp$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 <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
}
shinyApp(ui = ui, server = server)
{
"collab_server" : "",
"contents" : "",
"created" : 1571921116944.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "2538501940",
"id" : "B3E56EC9",
"lastKnownWriteTime" : 1571936330,
"last_content_update" : 1571936330757,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/7. Advance.R",
"project_path" : "7. Advance.R",
"properties" : {
"cursorPosition" : "5,14",
"scrollLine" : "0"
},
"relative_order" : 7,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#remove missing values
df <- na.omit(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")
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"))
)
## 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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10])))),
#Addition of a side bar menu to navigate to different tabs
sidebarMenu(
menuItem("Plots", tabName = "plots"),
menuItem("Table", tabName = "table")
)
)
#Adding components to body:
body <- dashboardBody(
tabItems(
tabItem("plots", row1, row2),
tabItem("table", dataTableOutput('table'))
))
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered() %>% filter(DISTRICT %in% (filtered() %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered() %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
output$table <- renderDataTable(
filtered(),
options = list(scrollX = TRUE)
)
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
{
"collab_server" : "",
"contents" : "",
"created" : 1571850299833.000,
"dirty" : false,
"encoding" : "UTF-8",
"folds" : "",
"hash" : "1187509367",
"id" : "D664F6C4",
"lastKnownWriteTime" : 1571925648,
"last_content_update" : 1571925648650,
"path" : "C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/3. ShinyDashboard.R",
"project_path" : "3. ShinyDashboard.R",
"properties" : {
"cursorPosition" : "45,0",
"scrollLine" : "12"
},
"relative_order" : 3,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}
\ No newline at end of file
#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 Police")
#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)
{
"cursorPosition" : "138,0",
"scrollLine" : "111",
"tempName" : "Untitled1"
}
\ No newline at end of file
{
"cursorPosition" : "31,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "45,0",
"scrollLine" : "12"
}
\ No newline at end of file
{
"cursorPosition" : "47,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
}
\ No newline at end of file
{
"cursorPosition" : "73,0",
"scrollLine" : "67"
}
\ No newline at end of file
{
"cursorPosition" : "64,0",
"scrollLine" : "31"
}
\ No newline at end of file
{
"cursorPosition" : "32,0",
"scrollLine" : "9"
}
\ No newline at end of file
{
"cursorPosition" : "5,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "7,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "3,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "0,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "36,0",
"scrollLine" : "3"
}
\ No newline at end of file
{
"cursorPosition" : "78,0",
"scrollLine" : "0",
"tempName" : "Untitled6"
}
\ No newline at end of file
{
"cursorPosition" : "30,53",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "82,0",
"scrollLine" : "49"
}
\ No newline at end of file
{
"cursorPosition" : "46,55",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "37,24",
"scrollLine" : "22"
}
\ No newline at end of file
{
"cursorPosition" : "5,14",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "0,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "106,0",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "115,0",
"scrollLine" : "0",
"tempName" : "Untitled7"
}
\ No newline at end of file
{
"cursorPosition" : "25,0",
"scrollLine" : "19",
"tempName" : "Untitled4"
}
\ No newline at end of file
{
"cursorPosition" : "50,0",
"scrollLine" : "16"
}
\ No newline at end of file
{
"cursorPosition" : "130,0",
"scrollLine" : "97"
}
\ No newline at end of file
{
"cursorPosition" : "33,0",
"scrollLine" : "0",
"tempName" : "Untitled1"
}
\ No newline at end of file
{
"cursorPosition" : "59,62",
"scrollLine" : "0"
}
\ No newline at end of file
{
"cursorPosition" : "2,11",
"scrollLine" : "0",
"tempName" : "Untitled5"
}
\ No newline at end of file
{
"cursorPosition" : "26,0",
"scrollLine" : "0",
"tempName" : "Untitled2"
}
\ No newline at end of file
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F1.%20Preprocessing.R="C777DB4A"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F2.%20SimpleShinyApp.R="725158E"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F3.%20ShinyDashboard.R="31EDA652"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F4.%20UpdatedDashboard.R="3D6D07B9"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F5.%20UpdatedDashboard_withTabs.R="7E86B5E3"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F6.%20Final_Dashboard.R="DA893C6A"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2F7.%20Advance.R="9978B5D1"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FInteractive_Dashboards%2FAdvance.R="11F66727"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2F1.%20Preprocessing.R="996C255D"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2F2.%20SimpleShinyApp.R="642D5398"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2F3.%20ShinyDashboard.R="77A6324"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2F4.%20UpdatedDashboard.R="87094C22"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2F5.%20UpdatedDashboard_withTabs.R="EB7923CA"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2F6.%20Final_Dashboard.R="49194170"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2FFinal_Dashboard.R="C1D6F94C"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2FPreprocessing.R="DABAA839"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2FShinyDashboard.R="C4DB5C5E"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2FSimpleShinyApp.R="FC40F374"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2FUpdatedDashboard.R="ED446F2C"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20Final%2FUpdatedDashboard_withTabs.R="7502090B"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F1st.R="3E095C95"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F2nd.R="43CCD005"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F3rd.R="A45D2BB7"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F4th.R="3923804B"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F5th.R="6A8E94CA"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F6th.R="1A88722D"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2F7th.R="340440BD"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2F4th%20Webinar%2FWebinar%20work%2Ffinal.R="BE804CE4"
C%3A%2FUsers%2FPaklap.pk%2FDesktop%2FData%20Science%20Dojo%2FBootcamp%2B%2B%2FRepo%2Frstudio_webinar%2Ffirst_published_r%2Fparta.R="3C947E7E"
C:/Users/Paklap.pk/Desktop/4th Webinar/Interactive_Dashboards/Advance.R="A7A115B7"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/1st.R="FA106C60"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/2nd.R="E11FD119"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/3rd.R="91C07CC0"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/4th.R="61444EF2"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/5th.R="AC2C9979"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/6th.R="E8AAB7E9"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/7th.R="50BA84C5"
C:/Users/Paklap.pk/Desktop/4th Webinar/Webinar work/final.R="1A14DF9F"
C:/Users/Paklap.pk/Desktop/Data Science Dojo/Bootcamp++/Repo/rstudio_webinar/first_published_r/parta.R="A6F9CBA1"
......@@ -14,9 +14,12 @@ df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#view first few rows
head(df)
#remove missing values
df <- na.omit(df)
#view number of rows and columns
dim(df)
#view data types
str(df)
......@@ -26,6 +29,7 @@ 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)
......@@ -44,6 +48,3 @@ ui <- basicPage("My first Shiny App")
server <- function(input, output){}
shinyApp(ui = ui, server = server)
......@@ -12,6 +12,7 @@ ui <- fluidPage(
"2018" = "2018",
"2019" = "2019")),
#setting an output in form of plot
plotOutput("barplot")
......@@ -25,7 +26,6 @@ server <- function(input, output){
#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()
......@@ -34,3 +34,4 @@ server <- function(input, output){
shinyApp(ui = ui, server = server)
#Building a simple Shiny Dashboard
library(shinydashboard)
......@@ -14,7 +13,7 @@ body <- dashboardBody(
)
#Defining the header object
header <- dashboardHeader(title = "Boston Crimes Dataset")
header <- dashboardHeader(title = "Boston Police")
#Defining the sidebard object
sidebar <- dashboardSidebar(
......
......@@ -13,7 +13,7 @@ body <- dashboardBody(
)
)
header <- dashboardHeader(title = "Boston Crimes Dataset")
header <- dashboardHeader(title = "Boston Police")
sidebar <- dashboardSidebar(
......@@ -26,9 +26,8 @@ sidebar <- dashboardSidebar(
#Adding a dropdown menu within the sidebar
selectInput("offense",
"Offense Description:",
c("All",
unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))
"Offense Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
......@@ -40,15 +39,17 @@ 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()
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp, aes(x = df_fil_grp$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])
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
......
......@@ -30,9 +30,8 @@ sidebar <- dashboardSidebar(
"2019" = "2019")),
selectInput("offense",
"Offense Description:",
c("All",
unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))
"Offense Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
......@@ -46,25 +45,29 @@ 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()
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$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()
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$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()
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
ggplot(data = df_fil_grp,aes(x = df_fil_grp$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])
df_fil <- filter(df, YEAR == input$year)
df_fil_grp <- filter(df_fil, OFFENSE_CODE_GROUP == input$offense)
dm <- data.frame(latitude = df_fil_grp$Lat, longitude = df_fil_grp$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
......@@ -75,6 +78,5 @@ server <- function(input, output) {
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
......@@ -8,6 +8,9 @@ library(shinydashboard)
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#remove missing values
df <- na.omit(df)
#Preprocessing
df$YEAR <- as.factor(df$YEAR)
df$HOUR <- as.factor(df$HOUR)
......@@ -19,15 +22,20 @@ df$OCCURRED_ON_DATE <- as.POSIXct(df$OCCURRED_ON_DATE, format = "%Y-%m-%d %H:%M:
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
......@@ -57,8 +65,8 @@ sidebar <- dashboardSidebar(
"2019" = "2019")),
selectInput("offense",
"Offense Description:",
c(unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))
"Offense Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
)
......@@ -70,43 +78,42 @@ ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#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()
ggplot(data = filtered(), aes(x = filtered()$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()
ggplot(data = filtered(), aes(x = filtered()$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()
ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + 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")
ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#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()
filtered() %>% filter(DISTRICT %in% (filtered() %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered() %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
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])
dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
......
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)
df <- read.csv(file = "crime.csv", header = TRUE, sep=",")
#remove missing values
df <- na.omit(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")
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"))
)
## 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 Group:",
c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10])))),
#Addition of a side bar menu to navigate to different tabs
sidebarMenu(
menuItem("Plots", tabName = "plots"),
menuItem("Table", tabName = "table")
)
)
#Adding components to body:
body <- dashboardBody(
tabItems(
tabItem("plots", row1, row2),
tabItem("table", dataTableOutput('table'))
))
#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)
## Project Boston Crimes: Server
server <- function(input, output) {
#creating a reactive object
filtered <- reactive({
filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
})
#creating the plotOutput content
output$permonth <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
})
output$perday <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
})
output$perhour <- renderPlot({
ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
})
#Defining the timeseries plot
output$timeseries <- renderPlot({
ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
})
#Defining the newly created stacked plot
output$stacked <- renderPlot({
filtered() %>% filter(DISTRICT %in% (filtered() %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
OFFENSE_DESCRIPTION %in% (filtered() %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>%
ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
geom_bar(position = "fill")
})
output$mymap <- renderLeaflet({
dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%
addMarkers(
clusterOptions = markerClusterOptions()
) %>% setView(-71.1, 42.32, zoom = 12)
})
output$table <- renderDataTable(
filtered(),
options = list(scrollX = TRUE)
)
}
## Project Boston Crimes: Building Our App
shinyApp(ui, server)
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