4. UpdatedDashboard.R 1.56 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

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

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 Description:",
              c("All",
                unique(as.character(df$OFFENSE_DESCRIPTION[1:10]))))

)

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()
    
  })

  #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])

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

  })
  
}


shinyApp(ui = ui, server = server)