@@ -20,6 +20,8 @@ accident_desc <- function(row)
2020strs <- apply(accidents , 1 , accident_desc )
2121names(strs ) <- NULL
2222
23+ accidents $ text <- strs
24+
2325# summary plot munging
2426d2 <- accidents %> % group_by(as.factor(ym )) %> %
2527 summarise(n = n())
@@ -38,13 +40,13 @@ colnames(clean) <- c("Severity", "No. vehicles",
3840 " Road conditions" , " Special conditions" , " Postcode" )
3941
4042shinyServer(function (input , output , session ) {
41-
43+
4244 getData <- reactive({
4345 subset(accidents , a_date > = input $ dates [[1 ]] & a_date < = input $ dates [[2 ]])
4446 })
4547
4648 getAlpha <- reactive({
47- message(" alpha changed : " , input $ alpha )
49+ # message("alpha changed : ", input$alpha)
4850 input $ alpha
4951 })
5052
@@ -63,38 +65,23 @@ shinyServer(function(input, output, session) {
6365 })
6466
6567 output $ mymap <- renderLeaflet({
68+ # build base map on load
6669 ax <- getData()
70+
71+ l <- leaflet(data = ax ) %> %
72+ addTiles(urlTemplate = " http://openmapsurfer.uni-hd.de/tiles/roadsg/x={x}&y={y}&z={z}" ) %> %
73+ addTiles(' http://{s}.tile.openstreetmap.se/hydda/roads_and_labels/{z}/{x}/{y}.png' ,
74+ attribution = ' © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' ) %> %
75+ setView(lng = - 3.19 , lat = 55.95 , zoom = 13 ) # %>%
6776
68- # fillv <- if(input$color == "None") "black" else
69- # if(input$color == "Severity") pal[as.factor(ax$severity)] else
70- # if(input$color == "Casualties") pal[as.factor(ax$no_casualt)] else
71- # if(input$color == "Time") cont_pal[ax$a_time_hr] else
72- # if(input$color == "Vehicles") pal[as.factor(ax$no_vehicle)] else
73- # pal[as.factor(ax$speed_limi)]
74-
75- l <- leaflet(data = ax ) %> %
76- addTiles(urlTemplate = " http://openmapsurfer.uni-hd.de/tiles/roadsg/x={x}&y={y}&z={z}" ) %> %
77- addTiles(' http://{s}.tile.openstreetmap.se/hydda/roads_and_labels/{z}/{x}/{y}.png' ,
78- attribution = ' © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' ) %> %
79- setView(lng = - 3.19 , lat = 55.95 , zoom = 13 ) # %>%
80- # addCircleMarkers(~long, ~lat, radius=~(no_vehicle+.8)**1.5, fillOpacity=input$alpha,
81- # color=NA, popup=strs, weight=2, fillColor = fillv)
82-
77+ # stop spinner
78+ session $ sendCustomMessage(type = " map_done" , " done" )
8379
84- session $ sendCustomMessage(type = " map_done" , " done" )
85-
86- l
80+ l
8781 })
8882
89- # observe({
90- # ax <- getData()
91- # alpha <- getAlpha()
92- # leafletProxy("mymap", session, data=ax) %>%
93- # addCircleMarkers(~long, ~lat, radius=~(no_vehicle+.8)**1.5, fillOpacity=alpha,
94- # color=NA, popup=strs, fillColor = "black")
95- # })
96-
9783 observe({
84+ # modify map of changed input i.e. colour by
9885 ax <- getData()
9986 title <- input $ color
10087
@@ -104,32 +91,48 @@ shinyServer(function(input, output, session) {
10491 " Time" = list (var = " a_time_hr" , type = " int" ),
10592 " Vehicles" = list (var = " no_vehicle" , type = " int" ),
10693 " Speed limit" = list (var = " speed_limi" , type = " int" ),
107- list (var = " speed_limi" , type = " int" ))
108- message(col )
109-
94+ list (var = " none" , type = " none" ))
95+
11096 col_fn <- function (col ){
11197 if (col $ type != " none" ){
11298 if (col $ type == " int" ) {
11399 return (colorNumeric(" Set1" , domain = ax [[col $ var ]]))
114100 } else {
115101 return (colorFactor(" Set1" , domain = ax [[col $ var ]]))
116- }} else return ( function () " black" )
102+ }} else return ( function (... ) " black" )
117103 }
118104
119- leafletProxy(" mymap" , session , data = ax ) %> %
120- clearMarkers() %> %
121- addCircleMarkers(~ long , ~ lat , radius = ~ 1 + (no_vehicle ** 2 ), fillOpacity = getAlpha(),
122- color = NA , popup = strs , fillColor = ~ col_fn(col )(ax [[col $ var ]])) %> %
123- addLegend(" bottomleft" , pal = col_fn(col ), values = ax [[col $ var ]], title = title )
105+ if (col $ var == " none" ){
106+ l <- leafletProxy(" mymap" , session , data = ax ) %> %
107+ addCircleMarkers(~ long , ~ lat , radius = ~ 1 + (no_vehicle ** 1.5 ), fillOpacity = getAlpha(),
108+ color = NA , popup = ~ text , fillColor = " black" ,
109+ layerId = paste0(" p" , 1 : nrow(ax ))) %> %
110+ removeControl(layerId = " legend" )
111+
112+ } else {
113+
114+ l <- leafletProxy(" mymap" , session , data = ax ) %> %
115+ addCircleMarkers(~ long , ~ lat , radius = ~ 1 + (no_vehicle ** 1.5 ), fillOpacity = getAlpha(),
116+ color = NA , popup = ~ text , fillColor = ~ col_fn(col )(ax [[col $ var ]]),
117+ layerId = paste0(" p" , 1 : nrow(ax ))) %> %
118+ addLegend(" bottomleft" , pal = col_fn(col ), values = ax [[col $ var ]],
119+ title = title , layerId = " legend" )
120+ }
121+
122+ l
124123 })
125124
126125 output $ monthTotals <- renderPlot({
126+ d2 <- getData() %> % group_by(as.factor(ym )) %> %
127+ summarise(n = n())
128+ colnames(d2 ) <- c(" ym" , " n" )
129+
127130 print(ggplot(d2 , aes(x = zoo :: as.Date(zoo :: as.yearmon(ym )), y = n )) +
128- geom_area() + theme_minimal() +
129- labs(x = " " , y = " Recorded collisions\n per month" ) +
130- scale_y_continuous(expand = c(0 ,0 )))
131+ geom_area() + theme_minimal() +
132+ labs(x = " " , y = " Recorded collisions\n per month" ) +
133+ scale_y_continuous(expand = c(0 ,0 )))
131134 })
132-
135+
133136 output $ table <- DT :: renderDataTable({
134137 DT :: datatable(clean , filter = ' top' , options = list (
135138 pageLength = 10 , autoWidth = TRUE ))
0 commit comments