Commit 4feb82da by Sanjay Pant

Updates all files

parent c91a0908
File added
This diff is collapsed. Click to expand it.
[]
\ 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