/
app.R
129 lines (115 loc) · 4.1 KB
/
app.R
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
##################################
# Created by EPI-interactive
# 18 Feb 2020
# https://www.epi-interactive.com
##################################
library(leaflet)
library(sp)
library(raster)
library(shiny)
library(sf)
ui <- fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "css/style.css")
),
div(class="col-xs-3 sidebar",
div(fluidRow(h1("Choropleth Grid")),
hr(),
sliderInput(
"xSlider",
"Horizontal Grid Count",
min = 10,
max = 70,
value = 50
),
sliderInput(
"ySlider",
"Vertical Grid Count",
min = 10,
max = 70,
value = 50
)),
tags$img(src="images/Epi_Logo.png", width= "90%")
),
div(class="col-xs-9 main",
leafletOutput("gridMap", height = "65%", width = "75%")
)
)
server <- function(input, output) {
#Read in shape file
shapeData_raw <- st_read("shapes/Wellington_City_Council_Boundary.shp")
shapeData <- sf::as_Spatial(shapeData_raw)
#Read in data
data <- read.csv("data/Wellington_City_Sculptures.csv", stringsAsFactors = F)
# From the data, create coordinates and project them the same as the shape file
xy <- data.frame(lon = data$X, lat = data$Y)
data <- SpatialPointsDataFrame(
coords = xy,
data = data,
proj4string = CRS(proj4string(shapeData))
)
output$gridMap <- renderLeaflet({
# define boundaries of object
shapeExtent <- extent(bbox(shapeData))
# create the grid itself, within extent boundaries
shapeRaster <- raster(shapeExtent, ncol=input$ySlider, nrow=input$xSlider)
# give it the same projection as shapeData
projection(shapeRaster) <- CRS(proj4string(shapeData))
# convert into polygon
shapePoly <- rasterToPolygons(shapeRaster)
# Clip grid to match the general area of the shapeData
clip <- crop(shapePoly, shapeData)
# use the shapeData boundaries to create a better outline for the grid
map <- raster::intersect(clip, shapeData)
#match data count to grid
sculptureCount <- aggregate(x = data["FID"],
by = map,
FUN = length)
# define color bins
qpal <- colorBin("YlOrRd",
sculptureCount$FID,
bins = 6,
na.color = "#f0f0f0",
right = T)
labelContent <- paste0(
ifelse(!is.na(sculptureCount$FID), sculptureCount$FID, "No"),
ifelse(
sculptureCount$FID == 1 &
!is.na(sculptureCount$FID),
" Sculpture",
" Sculptures"
)
)
# Render the map
leaflet(sculptureCount,
options = leafletOptions(minZoom = 11)) %>%
addTiles() %>%
addPolygons(
fillColor = ~qpal(sculptureCount$FID),
weight = 1,
color = "white",
fillOpacity = 0.8,
label = labelContent,
highlight = highlightOptions(
weight = 1,
color = "black",
bringToFront = TRUE,
fillOpacity = 1
)
) %>%
addLegend(
values = ~ sculptureCount$FID,
pal = qpal,
na.label = "0",
labFormat = function(type, cuts) {
#remove overlapping labels
sapply(2:length(cuts), function(i){
paste(cuts[i-1]+1, "-", cuts[i])
})
},
title = "Sculpture Count"
)
})
}
# Run the application
shinyApp(ui = ui, server = server)