5. UpdatedDashboard_withTabs.R 2.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

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",
Sanjay Pant committed
33 34
              "Offense Group:",
              c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
35 36 37 38 39 40 41 42 43 44 45 46 47
  
)

#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({
Sanjay Pant committed
48 49 50
    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()
51 52 53
  })
  
  output$perday <- renderPlot({
Sanjay Pant committed
54 55 56
    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()
57 58 59
  })
  
  output$perhour <- renderPlot({
Sanjay Pant committed
60 61 62
    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()
63 64 65 66 67
  })
  

  output$mymap <- renderLeaflet({
    
Sanjay Pant committed
68 69 70
    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)
71 72 73 74 75 76 77 78 79 80 81 82
    
    leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%  
      addMarkers(
        clusterOptions = markerClusterOptions()
      ) %>% setView(-71.1, 42.32, zoom = 12)
    
  })
}


## Project Boston Crimes: Building Our App
shinyApp(ui, server)