-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_area_selection.R
More file actions
224 lines (187 loc) · 5.92 KB
/
app_area_selection.R
File metadata and controls
224 lines (187 loc) · 5.92 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
###############################################################################
#' Description: Area selection app
#'
#' Input: Rds file prepared by SA2_map_data_prep.R
#'
#' Output: Shiny app that permits interactive selection of areas
#'
#' Author: Simon Anastasiadis
#'
#' Dependencies: shiny, leaflet, and sf packages
#'
#' Notes:
#' - Uses code folding by headers (Alt + O to collapse all).
#'
#' Issues:
#'
#' History (reverse order):
#' 2025-01-27 SA v1
#' ############################################################################
## install required packages ---------------------------------------------- ----
sys_timeout = getOption("timeout")
options(timeout = 1000)
req_packages = c("shiny", "leaflet", "sf")
for(pp in req_packages){
if(pp %in% installed.packages())
next
install.packages(pp)
}
options(timeout = sys_timeout)
## setup ------------------------------------------------------------------ ----
library(sf)
library(leaflet)
library(shiny)
# Load shapefile (modify the path accordingly)
shape_data = readRDS(here::here("SA2_higher_geographies_2025.Rds"))
debug = FALSE
## user interface --------------------------------------------------------- ----
# Define UI
ui <- fluidPage(
# header
shiny::fluidRow(
shiny::column(width = 1),
shiny::column(
width = 11,
shiny::h2("Area Selector")
)
),
shiny::fluidRow(
# control panel
shiny::column(
width = 4,
shiny::wellPanel(
hr(),
p("Initial map loading can be slow. Please be patient."),
p(
"Click areas to add them to the selection. Click them again to remove",
"Changing the selection type will select multiple nearby areas at once"
),
hr(),
actionButton("reset", label = "Reset map selections", width = "75%"),
hr(),
fileInput("load", label = "Load previous export", width = "75%", accept = ".csv"),
hr(),
downloadButton("export", label = "Export current selection", width = "75%"),
hr(),
verbatimTextOutput("selected_polygons")
)
),
# map display
shiny::column(
width = 8,
shiny::wellPanel(
leafletOutput("map", width = "100%", height = "80vw")
)
)
)
)
## server ----------------------------------------------------------------- ----
# Define Server
server <- function(input, output, session) {
# Reactive value to store selected polygon IDs
selected_polygons <- reactiveVal(c())
# initial map ----
output$map <- renderLeaflet({
leaflet(shape_data) %>%
addTiles() %>%
addPolygons(
layerId = ~id,
color = "#979aa0",
opacity = 0.4,
fillOpacity = 0.7,
weight = 2,
highlight = highlightOptions(weight = 5, color = "#e8731b", bringToFront = TRUE)
)
})
# select polygon function ----
select_polygon = function(id){
leafletProxy("map") %>%
removeShape(id) %>%
addPolygons(
data = shape_data[shape_data$id == id,],
layerId = id,
color = "#6b9b5f",
opacity = 0.3,
fillOpacity = 0.7,
weight = 2,
highlight = highlightOptions(weight = 5, color = "#366a59", bringToFront = TRUE)
)
}
# unselect polygon function ----
unselect_polygon = function(id){
leafletProxy("map") %>%
removeShape(id) %>%
addPolygons(
data = shape_data[shape_data$id == id,],
layerId = id,
color = "#979aa0",
opacity = 0.3,
fillOpacity = 0.7,
weight = 2,
highlight = highlightOptions(weight = 5, color = "#e8731b", bringToFront = TRUE)
)
}
# Observe clicks to toggle selection ----
observeEvent(input$map_shape_click, {
clicked_id <- input$map_shape_click$id
if (clicked_id %in% selected_polygons()) {
# Remove the polygon if already selected
selected_polygons(setdiff(selected_polygons(), clicked_id))
unselect_polygon(clicked_id)
} else {
# Add the polygon if not selected
selected_polygons(c(selected_polygons(), clicked_id))
select_polygon(clicked_id)
}
})
# action button - reset ----
observeEvent(input$reset, {
for(sp_id in selected_polygons()){
unselect_polygon(sp_id)
}
# empty selected_polygons
selected_polygons(c())
})
# action button - load ----
observeEvent(input$load, {
req(file.exists(input$load$datapath))
loaded_df = read.csv(input$load$datapath, stringsAsFactors = FALSE)
req(is.data.frame(loaded_df))
req("SA2_code" %in% colnames(loaded_df))
# remove current polygons
for(sp_id in selected_polygons()){
unselect_polygon(sp_id)
}
# update selected polygons
selected_polygons(c())
selected_polygons(shape_data$id[shape_data$SA2_code %in% loaded_df$SA2_code])
# refresh uploaded polygons
for(sp_id in selected_polygons()){
select_polygon(sp_id)
}
})
## action button - export ----
output$export = downloadHandler(
filename = function(){
fn = Sys.time()
fn = substr(as.character(fn), 1, 19)
fn = gsub(":", "", fn)
fn = paste0("selected region ",fn,".csv")
},
content = function(file){
out_df = data.frame(
SA2_code = shape_data$SA2_code[shape_data$id %in% selected_polygons()],
SA2_name = shape_data$SA2_name[shape_data$id %in% selected_polygons()]
)
write.csv(out_df, file, row.names = FALSE)
}
)
## Display selected polygons ----
output$selected_polygons <- renderPrint({
req(debug)
selected_polygons() # Show selected polygon data
})
}
## execute app ------------------------------------------------------------ ----
# Run the application
shinyApp(ui, server)