Skip to content

Commit 5fa601b

Browse files
author
Ben Moore
committed
automatic legends now working
1 parent 9d0be79 commit 5fa601b

File tree

4 files changed

+65
-59
lines changed

4 files changed

+65
-59
lines changed

debug.R

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ library("leaflet")
44
ax <- readRDS("data/accidents.rds")
55

66
input <- list()
7-
input$color <- "Speed limit"
7+
input$color <- "none"
88
title <- input$color
99

1010
col <- switch(input$color,
@@ -13,7 +13,7 @@ col <- switch(input$color,
1313
"Time" = list(var="a_time_hr", type="int"),
1414
"Vehicles" = list(var="no_vehicle", type="int"),
1515
"Speed limit" = list(var="speed_limi", type="int"),
16-
list(var="speed_limi", type="int"))
16+
list(var="none", type="none"))
1717
message(col)
1818

1919
col_fn <- function(col){
@@ -22,18 +22,21 @@ col_fn <- function(col){
2222
return(colorNumeric("Set1", domain=ax[[col$var]]))
2323
} else {
2424
return(colorFactor("Set1", domain=ax[[col$var]]))
25-
}} else return( function() "black" )
25+
}} else return( function(...) "black" )
2626
}
2727

2828
map <- leaflet(ax) %>% addTiles()
2929

3030
pal <- colorNumeric("Set1", ax$speed_limi)
3131

32-
# map %>%
33-
# addCircleMarkers(data=ax, ~long, ~lat, radius=8, fillOpacity=.3,
34-
# color=NA, fillColor = ~pal(speed_limi))
35-
36-
map %>%
37-
addCircleMarkers(data=ax, ~long, ~lat, radius=8, fillOpacity=.3,
38-
color=NA, fillColor = ~col_fn(col)(ax[[col$var]])) %>%
39-
addLegend("bottomleft", pal=col_fn(col), values=ax[[col$var]], title=title)
32+
if(col[[1]] == "none"){
33+
map %>%
34+
addCircleMarkers(data=ax, ~long, ~lat, radius=8, fillOpacity=.3,
35+
color=NA, fillColor = ~col_fn(col)(ax[[col$var]]))
36+
} else {
37+
map %>%
38+
addCircleMarkers(data=ax, ~long, ~lat, radius=8, fillOpacity=.3,
39+
color=NA, fillColor = ~col_fn(col)(ax[[col$var]])) %>%
40+
addLegend("bottomleft", pal=col_fn(col), values=ax[[col$var]], title=title,
41+
layerId="legend")
42+
}

server.R

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ accident_desc <- function(row)
2020
strs <- apply(accidents, 1, accident_desc)
2121
names(strs) <- NULL
2222

23+
accidents$text <- strs
24+
2325
# summary plot munging
2426
d2 <- 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

4042
shinyServer(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='&copy; <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='&copy; <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\nper month") +
130-
scale_y_continuous(expand=c(0,0)))
131+
geom_area() + theme_minimal() +
132+
labs(x="", y="Recorded collisions\nper 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))

styles.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ h1, h2, h3, h4 { font-weight: 400; }
2727
padding: 0 20px 20px 20px;
2828
cursor: move;
2929
/* Fade out while not hovering */
30-
opacity: 0.65;
30+
opacity: 0.7;
3131
zoom: 0.9;
3232
transition: opacity 500ms 1s;
3333
}
3434
#controls:hover {
3535
/* Fade in while hovering */
36-
opacity: 0.95;
36+
opacity: 1;
3737
transition-delay: 0;
3838
}
3939

ui.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ $( 'div#mymap' ).append(spinner.el);"),
3939
label = 'Occurred between:',
4040
start = as.Date("2010-01-01"), end = as.Date("2013-07-01")),
4141

42-
selectInput("color", "Color by:",
42+
selectInput("color", "Colour by:",
4343
choices=c("None", "Severity", "Casualties", "Time", "Vehicles", "Speed limit")),
4444

4545
sliderInput("alpha", "Opacity:",
46-
min=0, max=1, value=.2, step=.01, ticks=F),
46+
min=0, max=1, value=.4, step=.025, ticks=T),
4747

4848
hr(),
4949
h4("Summary plots"),
@@ -65,10 +65,10 @@ $( 'div#mymap' ).append(spinner.el);"),
6565
$( "div#mymap" ).remove(spinner);
6666
});')
6767

68-
),
68+
)#,
6969

70-
tags$div(id="cite",
71-
a("@benjaminlmoore", href="http://twitter.com/benjaminlmoore"))
70+
# tags$div(id="cite",
71+
# a("@benjaminlmoore", href="http://twitter.com/benjaminlmoore"))
7272
)
7373
), tabPanel("Table", DT::dataTableOutput("table"))
7474
)

0 commit comments

Comments
 (0)