I am working with R in RStudio and created a shiny dashboard. Within the dashboard I am using the leaflet() function to plot a map. Now I am trying to put markers on the map which shows the crimes in chicago. I am working with the selectInput() and sliderInput() function to select different crime type, location and year. If I select something inside the input field the markers are shown up on the chicago map and it works perfect. But I want that when I start the shiny app all markers are displayed without filtering based on selectInput(). This is my ui.R code:
tabItem(tabName = "map",
div(class="outer",
tags$head(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"))),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(id = "mapControls", fixed = TRUE, draggable = TRUE, top = 150, left = "auto", right = 15, bottom = "auto", width = 200, height = "auto",
selectInput("mapCrimeType", label= "Select Crime Type", choices = unique(cc$Primary.Type), multiple = TRUE),
selectInput("mapLocation", label= "Select Location", choices = unique(cc$Location.Description), multiple = TRUE),
sliderInput("mapYear", label = "Select Year", min = 2001, max = 2016, step = 1, sep = '', value = c(2001,2016))
)
),
And this my server.R code:
server <- function(input, output) {
### Draw Map ###
output$map = renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
setView(lng = -87.6105, lat = 41.8947, zoom=11)
})
reactMap = reactive({
cc %>%
filter(Primary.Type %in% input$mapCrimeType &
Location.Description %in% input$mapLocation &
Year %in% cbind(input$mapYear[1],input$mapYear[2]))
})
observe({
proxy = leafletProxy("map", data = reactMap()) %>%
clearMarkers() %>%
clearMarkerClusters() %>%
addCircleMarkers(clusterOptions = markerClusterOptions(),
lng =~ Longitude, lat =~ Latitude, radius = 5, group = 'Cluster',
popup =~ paste('<b><font color="Black">', 'Crime Information',
'</font></b><br/>', 'Crime Type:', Primary.Type,'<br/>',
'Date:', Date,'<br/>', #'Time:', Time,'<br/>',
'Location:', Location.Description,'<br/>', 'Block:', Block, '<br/>', 'Arrest:', Arrest, '<br/>'))
})
I guess I have to change something in the reactive function inside the server.R file. I hope somebody can help me.
UPDATE: Working with this dataset: https://www.kaggle.com/currie32/crimes-in-chicago