-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
442 lines (371 loc) · 13.8 KB
/
Copy pathapp.R
File metadata and controls
442 lines (371 loc) · 13.8 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# ---- Install & load required packages ----
required_pkgs <- c(
"shiny",
"shinyFiles",
"shinycssloaders",
"data.table",
"openxlsx",
"zip"
)
install_if_missing <- function(pkgs) {
to_install <- pkgs[!pkgs %in% installed.packages()[, "Package"]]
if (length(to_install) > 0) {
install.packages(
to_install,
dependencies = TRUE,
repos = "https://cloud.r-project.org"
)
}
}
load_packages <- function(pkgs) {
for (p in pkgs) {
suppressPackageStartupMessages(library(p, character.only = TRUE))
}
}
install_if_missing(required_pkgs)
load_packages(required_pkgs)
# =====================================================================
# 1. Dynamic Filter Module (UI)
# =====================================================================
filterInputUI <- function(id, label) {
ns <- NS(id)
selectizeInput(
ns("value"),
label,
choices = NULL,
multiple = TRUE,
options = list(
plugins = list("remove_button"),
placeholder = paste("Select", label),
persist = TRUE
)
)
}
# =====================================================================
# 2. Dynamic Filter Module (Server)
# =====================================================================
filterInputServer <- function(id, data, column) {
moduleServer(id, function(input, output, session) {
observe({
req(data())
col_data <- data()[[column]]
choices <- c("All", sort(unique(col_data)))
updateSelectizeInput(
session, "value",
choices = choices,
selected = "All",
server = TRUE
)
})
reactive({
req(data())
if ("All" %in% input$value) unique(data()[[column]])
else input$value
})
})
}
# =====================================================================
# 3. User Interface
# =====================================================================
ui <- fluidPage(
titlePanel("ISMC Data Export"),
sidebarLayout(
sidebarPanel(
# --- PSP or Non-PSP ---
radioButtons(
"psp", "PSP or Non-PSP",
choices = c("nonPSP", "PSP"),
selected = "nonPSP",
inline = TRUE
),
# --- Compilation Date ---
selectizeInput(
"compdate",
"Compilation Date",
choices = NULL,
multiple = FALSE
),
# --- Dynamic Filters (Modules) ---
filterInputUI("sampltype", "Sample Type"),
filterInputUI("tsa", "TSA"),
filterInputUI("mgmtunit", "Management Unit"),
filterInputUI("ownership", "Ownership"),
filterInputUI("sitecode", "Site Purpose"),
selectizeInput(
"ysm",
"YSM",
choices = c("All", "YSM Pilot only"), selected = "All",
multiple = F,
options = list('plugins' = list('remove_button'),
placeholder = 'Select YSM', 'persist' = TRUE)
),
sliderInput("year", "Year Range", min = 1900, max = 2025,
value = c(1900, 2025), step = 1, sep = ""),
textAreaInput("site_list",
"Enter Site IDs (comma or line separated):",
placeholder = "e.g. 2095167, 2097163, 2095157",
rows = 3),
# Export button + Download link
actionButton("db", "Export Data", width = "100%"),
# Optional: text output to show download ready
#textOutput("download_status") %>% withSpinner(type = 6, color = "#0072B2")
tags$head(tags$script(HTML('
Shiny.addCustomMessageHandler("jsCode",
function(message) { eval(message.value); });
'))),
downloadLink("downloadCSV", label="")
),
mainPanel(
tableOutput("setting"),
#textOutput("samplesize"),
tableOutput("table1"),
tableOutput("table2")
)
)
)
# =====================================================================
# 4. Server Logic
# =====================================================================
server <- function(input, output, session) {
# IMPORTANT!
# this is needed to terminate the R process when the
# shiny app session ends. Otherwise, you end up with a zombie process
session$onSessionEnded(function() {
stopApp()
})
# -------------------------------------------
# PSP / non-PSP + Compilation Dates
# -------------------------------------------
datapath <- "//objectstore3.nrs.bcgov/s164/S63016/!Workgrp/Inventory/Compilation/ismc/forpublish"
pspornot <- reactive({
if (input$psp == "PSP") "PSP" else "nonPSP"
})
compdate_list <- reactive({
if (input$psp == "PSP") {
as.numeric(gsub("PSP_", "", dir(datapath, pattern="^PSP_", recursive=FALSE)))
} else {
as.numeric(gsub("nonPSP_", "", dir(datapath, pattern="^nonPSP_", recursive=FALSE)))
}
})
latest_compdate <- reactive({ max(compdate_list()) })
# Update compilation date selector when PSP changes
observeEvent(input$psp, {
freezeReactiveValue(input, "compdate")
choices <- if (input$psp == "PSP") {
c("Latest", compdate_list())
} else {
c("Latest", compdate_list())
}
updateSelectizeInput(session, "compdate", choices = choices, selected = "Latest")
})
compdate <- reactive({
if (input$compdate != "Latest") input$compdate
else latest_compdate()
})
filepath <- reactive({
paste0(datapath, "/", pspornot(), "_", compdate())
})
# -------------------------------------------
# Load samples & header from selected path
# -------------------------------------------
samples <- reactive({
fread(file.path(filepath(), "faib_sample_byvisit.csv"))
})
sites <- reactive({
fread(file.path(filepath(), "faib_header.csv"))
})
sample_site <- reactive({
merge(
samples(),
sites()[, !"SAMPLE_ESTABLISHMENT_TYPE"],
by = "SITE_IDENTIFIER"
)
})
# -------------------------------------------
# Filters (Modules)
# -------------------------------------------
sampltype <- filterInputServer("sampltype", sample_site, "SAMPLE_ESTABLISHMENT_TYPE")
tsa <- filterInputServer("tsa", sample_site, "TSA_DESC")
mgmtunit <- filterInputServer("mgmtunit", sample_site, "MGMT_UNIT")
ownership <- filterInputServer("ownership", sample_site, "OWN_SCHED_DESCRIP")
sitecode <- filterInputServer("sitecode", sample_site, "SAMPLE_SITE_PURPOSE_TYPE_CODE")
# YSM filter
ysm <- reactive({
if ("All" %in% input$ysm) c("Y", "N", "", NA) else "Y"
})
year <- reactive(input$year)
# Manual list
manual_list <- reactive({
if (is.null(input$site_list) || input$site_list == "") {
return(NULL)
}
site_id <- unlist(strsplit(input$site_list, "[^[:alnum:]_]+"))
site_id <- trimws(site_id)
site_id <- site_id[nzchar(site_id)]
unique(as.numeric(site_id))
})
# -------------------------------------------
# Selected samples
# -------------------------------------------
selected_sample <- reactive({
ss <- sample_site()
ss[
SAMPLE_ESTABLISHMENT_TYPE %in% sampltype() &
TSA_DESC %in% tsa() &
MGMT_UNIT %in% mgmtunit() &
OWN_SCHED_DESCRIP %in% ownership() &
SAMPLE_SITE_PURPOSE_TYPE_CODE %in% sitecode() &
MEAS_YR >= input$year[1] &
MEAS_YR <= input$year[2] &
YSM_PILOT_FM %in% ysm(),
]
# apply manual filter ONLY if provided
site_ids <- manual_list()
if (!is.null(site_ids)) {
ss <- ss[SITE_IDENTIFIER %in% site_ids]
}
ss[, CLSTR_ID]
})
selected_site <- reactive({
ss <- sample_site()
ss <- ss[
SAMPLE_ESTABLISHMENT_TYPE %in% sampltype() &
TSA_DESC %in% tsa() &
MGMT_UNIT %in% mgmtunit() &
OWN_SCHED_DESCRIP %in% ownership() &
MEAS_YR >= year()[1] &
MEAS_YR <= year()[2] &
SAMPLE_SITE_PURPOSE_TYPE_CODE %in% sitecode() &
(YSM_PILOT_FM %in% ysm() | YSM_PILOT_LM %in% ysm())
]
# apply manual filter only if provided
site_ids <- manual_list()
if (!is.null(site_ids)) {
ss <- ss[SITE_IDENTIFIER %in% site_ids]
}
ss[, SITE_IDENTIFIER]
})
# -------------------------------------------
# Example Outputs
# -------------------------------------------
output$setting <- renderTable({
data.frame(Data = c("Location", "PSP or non-PSP",
"Compilation Date"
),
Source = c(paste(datapath), paste(pspornot()),
paste(compdate())
))
})
output$table1 <- renderTable({
# Helper to collapse input safely
collapse_safe <- function(x, all_values = NULL, sep = ", ") {
if (is.null(x) || length(x) == 0) return("")
if (!is.null(all_values) && length(x) == length(all_values)) return("All")
paste(x, collapse = sep)
}
data.frame(
Field = c(
"SAMPLE_ESTABLISHMENT_TYPE",
"TSA_DESC",
"MGMT_UNIT",
"OWN_SCHED_DESCRIP",
"MEAS_YR",
"SAMPLE_SITE_PURPOSE_TYPE_CODE",
"YSM",
"Manually Listed Site IDs"
),
#Input = c(
# collapse_safe(input$sampltype),
# collapse_safe(input$tsa),
# collapse_safe(input$mgmtunit),
# collapse_safe(input$ownership),
# collapse_safe(input$year, sep = "-"),
# collapse_safe(input$sitecode, sep = "-"),
# collapse_safe(input$ysm)
#),
Input = c(
collapse_safe(sampltype(), all_values = unique(sample_site()$SAMPLE_ESTABLISHMENT_TYPE)),
collapse_safe(tsa(), all_values = unique(sample_site()$TSA_DESC)),
collapse_safe(mgmtunit(), all_values = unique(sample_site()$MGMT_UNIT)),
collapse_safe(ownership(), all_values = unique(sample_site()$OWN_SCHED_DESCRIP)),
paste(year(), collapse = "-"), # year usually no "All"
collapse_safe(sitecode(), all_values = unique(sample_site()$SAMPLE_SITE_PURPOSE_TYPE_CODE)),
ifelse(all(ysm() == "Y"), "YSM Pilot only", "All"),
paste0(manual_list(), collapse = ", ")
),
stringsAsFactors = FALSE
)
})
output$table2 <- renderTable({
data.frame(Name = c("Total sites:", "Selected sites:",
"Total samples:", "Selected samples:"),
Number = c(nrow(sites()), length(unique(selected_site())),
nrow(samples()), length(selected_sample()))
)
})
observeEvent(input$db, {
# Show progress
withProgress(message = "Preparing export files...", value = 0, {
incProgress(0.1, detail = "Loading summary tables")
summary <- fread(file.path(filepath(), "faib_compiled_smries.csv"))
summary_ht <- fread(file.path(filepath(), "faib_compiled_smries_ht.csv"))
#summary_wk <- fread(file.path(filepath(), "faib_compiled_smries_wk.csv"))
summary_spc <- fread(file.path(filepath(), "faib_compiled_spcsmries.csv"))
summary_siteage <- fread(file.path(filepath(), "faib_compiled_spcsmries_siteage.csv"))
#summary_spc_wk <- fread(file.path(filepath(), "faib_compiled_spcsmries_wk.csv"))
plot_header <- fread(file.path(filepath(), "faib_plot_header.csv"))
tree <- fread(file.path(filepath(), "faib_tree_detail.csv"))
wb <- openxlsx::loadWorkbook(file.path(filepath(), "data_dictionary.xlsx"))
if (pspornot() != "PSP") {
summary_wk <- fread(file.path(filepath(), "faib_compiled_smries_wk.csv"))
summary_spc_wk <- fread(file.path(filepath(), "faib_compiled_spcsmries_wk.csv"))
}
incProgress(0.3, detail = "Writing temporary CSV files")
tmp_files <- list(
"faib_sample_byvisit.csv" = samples()[CLSTR_ID %in% selected_sample(), ],
"faib_header.csv" = sites()[SITE_IDENTIFIER %in% selected_site(), ],
"faib_compiled_smries.csv" = summary[SITE_IDENTIFIER %in% selected_site(), ],
"faib_compiled_smries_ht.csv" = summary_ht[CLSTR_ID %in% selected_sample(), ],
"faib_compiled_spcsmries.csv" = summary_spc[CLSTR_ID %in% selected_sample(), ],
"faib_compiled_spcsmries_siteage.csv" = summary_siteage[CLSTR_ID %in% selected_sample(), ],
"faib_plot_header.csv" = plot_header[CLSTR_ID %in% selected_sample(), ],
"faib_tree_detail.csv" = tree[CLSTR_ID %in% selected_sample(), ]
)
# Conditionally add weekly summaries if not PSP
if (pspornot() != "PSP") {
tmp_files[["faib_compiled_smries_wk.csv"]] <- summary_wk[CLSTR_ID %in% selected_sample(), ]
tmp_files[["faib_compiled_spcsmries_wk.csv"]] <- summary_spc_wk[CLSTR_ID %in% selected_sample(), ]
}
# Write CSVs
for(f in names(tmp_files)) {
fwrite(tmp_files[[f]], file = f)
}
# Save Excel workbook
saveWorkbook(wb, file = "data_dictionary.xlsx", overwrite = T)
incProgress(0.6, detail = "Zipping files")
files_to_zip <- c(names(tmp_files), "data_dictionary.xlsx")
zipfile <- paste0(tempfile(), ".zip")
zip::zip(zipfile = zipfile, files = files_to_zip) # -j flattens structure
# Remove temporary files
#file_remove(files_to_zip)
incProgress(1, detail = "Ready for download")
# Trigger download
output$downloadCSV <- downloadHandler(
filename = paste0("bc_custom_sample_data_", Sys.Date(), ".zip"),
content = function(fname) {
file.copy(zipfile, fname)
},
contentType = "application/zip"
)
# Update UI message
output$download_status <- renderText("Click the link below to download:")
# Optional: auto-click download link using JS
jsinject <- "setTimeout(function(){window.open($('#downloadCSV').attr('href'))}, 100);"
session$sendCustomMessage(type = 'jsCode', list(value = jsinject))
})
})
}
# =====================================================================
# 5. Run App
# =====================================================================
shinyApp(ui, server)