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')