4. UpdatedDashboard.R 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

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

Sanjay Pant committed
16
header <- dashboardHeader(title = "Boston Police")
17 18 19 20 21 22 23 24 25 26 27 28

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",
Sanjay Pant committed
29 30
              "Offense Group:",
              c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
31 32 33 34 35 36 37 38 39 40 41

)

ui <- dashboardPage(header, sidebar, body)


server <- function(input, output) {
  
  output$barplot <- renderPlot({
    
    df_fil <- filter(df, YEAR == input$year)
Sanjay Pant committed
42 43
    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()
44 45 46 47 48 49
    
  })

  #Defining our 'mapview' plot declared in the UI above
  output$mapview <- renderLeaflet({
    
Sanjay Pant committed
50 51 52
    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)
53 54 55 56 57 58 59 60 61 62 63 64 65

      leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%  
      addMarkers(
        clusterOptions = markerClusterOptions()
      ) %>% setView(-71.1, 42.32, zoom = 12)

  })
  
}


shinyApp(ui = ui, server = server)