-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampling_calendar_app.R
More file actions
325 lines (273 loc) Β· 13.5 KB
/
Copy pathSampling_calendar_app.R
File metadata and controls
325 lines (273 loc) Β· 13.5 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
library(shiny)
library(dplyr)
library(ggplot2)
library(lubridate)
library(lunar)
library(rlang)
# Define the UI with scrolling sidebar for inputs ####
ui <- fluidPage(
tags$head(
# CSS for making the sidebar scrollable
tags$style(HTML("
.scrollable-sidebar {
max-height: 600px; /* Set the maximum height for the sidebar */
overflow-y: auto; /* Enable vertical scrolling */
}
"))
),
titlePanel("Sampling Calendar Visualization"),
sidebarLayout(
sidebarPanel(
div(class = "scrollable-sidebar", # Apply scrolling class to the sidebar panel
dateInput("start_date", "Start Date:", value = "2025-01-01"),
dateInput("end_date", "End Date:", value = "2025-12-20"),
# Step 1: Complete list of sampling zones input
textInput(
"all_zones",
"Complete List of Sampling Zones (comma-separated):",
value = ""
),
# Step 2: Checkbox groups for assigning zones to sampling types
uiOutput("zone_selection_plankton"),
uiOutput("zone_selection_eDNA"),
uiOutput("zone_selection_eDNA2"),
uiOutput("zone_selection_sediment"),
uiOutput("zone_selection_sediment2"),
# Number of zones in Other zones
numericInput("n_otherzones", "Number of zones under the Other_zones umbrella:", value = 20, min = 1, max = 1000),
# Number of replicas input
numericInput("nrep_plankton", "Number of Replicas for Plankton:", value = 3, min = 1, max = 10),
numericInput("nrep_eDNA", "Number of Replicas for eDNA:", value = 3, min = 1, max = 10),
numericInput("nrep_eDNA2", "Number of Replicas for eDNA2:", value = 1, min = 1, max = 10),
numericInput("nrep_sediment", "Number of Replicas for Sediment:", value = 3, min = 1, max = 10),
numericInput("nrep_sediment2", "Number of Replicas for Sediment2:", value = 1, min = 1, max = 10),
# Sampling frequencies input
textInput("freq_plankton", "Frequency for Plankton:", value = "2 weeks"),
textInput("freq_eDNA", "Frequency for eDNA:", value = "13 weeks"),
textInput("freq_eDNA2", "Frequency for eDNA2:", value = "52 weeks"),
textInput("freq_sediment", "Frequency for Sediment:", value = "26 weeks"),
textInput("freq_sediment2", "Frequency for Sediment2:", value = "52 weeks"),
# Delays input
numericInput("delay_plankton", "Delay for Plankton (weeks):", value = 0, min = 0),
numericInput("delay_eDNA", "Delay for eDNA (weeks):", value = 4, min = 0),
numericInput("delay_eDNA2", "Delay for eDNA2 (weeks):", value = 17, min = 0),
numericInput("delay_sediment", "Delay for Sediment (weeks):", value = 17, min = 0),
numericInput("delay_sediment2", "Delay for Sediment2 (weeks):", value = 17, min = 0)
)
),
mainPanel(
plotOutput("samplingPlot"),
tableOutput("samplingTable")
)
)
)
# Define the server logic ####
server <- function(input, output, session) {
# Load pre-defined zones from a CSV file ####
zones_data <- reactive({
# Ensure the file exists and load the data correctly
req(file.exists("zone_coords.csv")) # Ensure the file exists
read.csv("zone_coords.csv")
})
# Update the all_zones input dynamically based on the loaded file
observe({
zones <- zones_data()$zone
updateTextInput(session, "all_zones", value = paste(zones, collapse = ","))
})
# Create reactive list of zones based on user input
all_zones_reactive <- reactive({
req(input$all_zones) # Ensure input is available
unlist(strsplit(input$all_zones, ","))
})
# Create reactive function to extract pre-selected zones given sample types
preselected_reactive <- function(zone_selection = "zone_selection_plankton") {
reactive({
zones_data() %>%
filter(!!sym(zone_selection)) %>% # Dynamically use the column name for filtering
pull(zone) %>% # Extract the 'zone' column
unlist() # Convert to a character vector
})
}
# Generate UI selection checkboxs for each sampling type ####
output$zone_selection_plankton <- renderUI({
checkboxGroupInput(
"zones_plankton",
"Sampling Zones for Plankton:",
choices = all_zones_reactive(),
selected = preselected_reactive("zone_selection_plankton")() # Call the reactive function and evaluate it
)
})
output$zone_selection_eDNA <- renderUI({
checkboxGroupInput(
"zones_eDNA",
"Sampling Zones for eDNA:",
choices = all_zones_reactive(),
selected = preselected_reactive("zone_selection_eDNA")()
)
})
output$zone_selection_eDNA2 <- renderUI({
checkboxGroupInput(
"zones_eDNA2",
"Sampling Zones for eDNA2:",
choices = all_zones_reactive(),
selected = preselected_reactive("zone_selection_eDNA2")()
)
})
output$zone_selection_sediment <- renderUI({
checkboxGroupInput(
"zones_sediment",
"Sampling Zones for Sediment:",
choices = all_zones_reactive(),
selected = preselected_reactive("zone_selection_sediment")()
)
})
output$zone_selection_sediment2 <- renderUI({
checkboxGroupInput(
"zones_sediment2",
"Sampling Zones for Sediment2:",
choices = all_zones_reactive(),
selected = preselected_reactive("zone_selection_sediment2")()
)
})
# Reactive function to create the sampling calendar ####
sampling_calendar_reactive <- reactive({
# Convert checkbox selections to vectors for zones
sampling_zones_plankton <- input$zones_plankton
sampling_zones_eDNA <- input$zones_eDNA
sampling_zones_eDNA2 <- input$zones_eDNA2
sampling_zones_sediment <- input$zones_sediment
sampling_zones_sediment2 <- input$zones_sediment2
start_date <- input$start_date
end_date <- input$end_date
# Define delays using lubridate weeks function
delay_plankton <- weeks(input$delay_plankton)
delay_eDNA <- weeks(input$delay_eDNA)
delay_eDNA2 <- weeks(input$delay_eDNA2)
delay_sediment <- weeks(input$delay_sediment)
delay_sediment2 <- weeks(input$delay_sediment2)
# Create a backbone weekly calendar
week_ID <- seq.Date(from = start_date, to = end_date, by = "week")
moon_phase <- lunar.phase(week_ID, name = TRUE)
weekly_schema <- data.frame(
week_ID = 1:length(week_ID),
week_start_date = week_ID,
moon_phase = moon_phase
)
# Calculate sampling dates for each sample type
sampling_dates_plankton <- seq.Date(from = start_date + delay_plankton, to = end_date, by = input$freq_plankton)
sampling_dates_eDNA <- seq.Date(from = start_date + delay_eDNA, to = end_date, by = input$freq_eDNA)
sampling_dates_eDNA2 <- seq.Date(from = start_date + delay_eDNA2, to = end_date, by = input$freq_eDNA2)
sampling_dates_sediment <- seq.Date(from = start_date + delay_sediment, to = end_date, by = input$freq_sediment)
sampling_dates_sediment2 <- seq.Date(from = start_date + delay_sediment2, to = end_date, by = input$freq_sediment2)
# Add sampling flags to 'weekly_schema'
weekly_schema <- weekly_schema %>%
mutate(sampling_flag_plankton = ifelse(week_start_date %in% sampling_dates_plankton, TRUE, FALSE),
sampling_flag_eDNA = ifelse(week_start_date %in% sampling_dates_eDNA, TRUE, FALSE),
sampling_flag_eDNA2 = ifelse(week_start_date %in% sampling_dates_eDNA2, TRUE, FALSE),
sampling_flag_sediment = ifelse(week_start_date %in% sampling_dates_sediment, TRUE, FALSE),
sampling_flag_sediment2 = ifelse(week_start_date %in% sampling_dates_sediment2, TRUE, FALSE))
# Create a list to store each sampling calendar
sampling_calendars <- list()
# Expand data frames for each sample type
if (!is.null(sampling_zones_plankton) && length(sampling_zones_plankton) > 0) {
sampling_calendar_plankton <- expand.grid(week_ID = weekly_schema$week_ID, zone = sampling_zones_plankton) %>%
left_join(weekly_schema, by = "week_ID") %>%
mutate(sampling = ifelse(sampling_flag_plankton, "Plankton", "No sampling"),
n_replicas = ifelse(sampling_flag_plankton, input$nrep_plankton, 0))
sampling_calendars[[length(sampling_calendars) + 1]] <- sampling_calendar_plankton %>%
mutate(sampling_calendar = "Plankton")
}
if (!is.null(sampling_zones_eDNA) && length(sampling_zones_eDNA) > 0) {
sampling_calendar_eDNA <- expand.grid(week_ID = weekly_schema$week_ID, zone = sampling_zones_eDNA) %>%
left_join(weekly_schema, by = "week_ID") %>%
mutate(sampling = ifelse(sampling_flag_eDNA, "eDNA", "No sampling"),
n_replicas = ifelse(sampling_flag_eDNA, input$nrep_eDNA, 0))
sampling_calendars[[length(sampling_calendars) + 1]] <- sampling_calendar_eDNA %>%
mutate(sampling_calendar = "eDNA")
}
if (!is.null(sampling_zones_eDNA2) && length(sampling_zones_eDNA2) > 0) {
sampling_calendar_eDNA2 <- expand.grid(week_ID = weekly_schema$week_ID, zone = sampling_zones_eDNA2) %>%
left_join(weekly_schema, by = "week_ID") %>%
mutate(sampling = ifelse(sampling_flag_eDNA2, "eDNA", "No sampling"),
n_replicas = ifelse(sampling_flag_eDNA2, input$nrep_eDNA2, 0))
sampling_calendars[[length(sampling_calendars) + 1]] <- sampling_calendar_eDNA2 %>%
mutate(sampling_calendar = "eDNA")
}
if (!is.null(sampling_zones_sediment) && length(sampling_zones_sediment) > 0) {
sampling_calendar_sediment <- expand.grid(week_ID = weekly_schema$week_ID, zone = sampling_zones_sediment) %>%
left_join(weekly_schema, by = "week_ID") %>%
mutate(sampling = ifelse(sampling_flag_sediment, "Sediment", "No sampling"),
n_replicas = ifelse(sampling_flag_sediment, input$nrep_sediment, 0))
sampling_calendars[[length(sampling_calendars) + 1]] <- sampling_calendar_sediment %>%
mutate(sampling_calendar = "Sediment")
}
if (!is.null(sampling_zones_sediment2) && length(sampling_zones_sediment2) > 0) {
sampling_calendar_sediment2 <- expand.grid(week_ID = weekly_schema$week_ID, zone = sampling_zones_sediment2) %>%
left_join(weekly_schema, by = "week_ID") %>%
mutate(sampling = ifelse(sampling_flag_sediment2, "Sediment", "No sampling"),
n_replicas = ifelse(sampling_flag_sediment2, input$nrep_sediment2, 0))
sampling_calendars[[length(sampling_calendars) + 1]] <- sampling_calendar_sediment2 %>%
mutate(sampling_calendar = "Sediment")
}
# Combine into one comprehensive calendar, only bind rows if data frames are non-NULL
if (length(sampling_calendars) > 0) {
sampling_calendar <- bind_rows(sampling_calendars) %>%
mutate(zone = factor(zone, levels = rev(unique(zone))),
sampling_calendar = factor(sampling_calendar, levels = c("Plankton", "eDNA", "Sediment")),
sampling = factor(sampling, levels = c("No sampling", "Plankton", "eDNA", "Sediment")))
} else {
sampling_calendar <- NULL # Return NULL if no sampling data is available
}
return(sampling_calendar)
})
# Render the calendar plot ####
output$samplingPlot <- renderPlot({
sampling_calendar <- sampling_calendar_reactive()
# Make sure sampling_calendar is ready before moving on to avoid errors.
if (is.null(sampling_calendar) || nrow(sampling_calendar) == 0) {
return(NULL) # Return nothing if no sampling calendar is available
}
date_labels <- sampling_calendar %>%
group_by(date_labels = format(week_start_date, "%b-%Y")) %>%
summarize(week_ID = min(week_ID))
moon_phase_labels <- sampling_calendar %>%
select(week_ID, moon_phase) %>%
mutate(moon_phase_symbol = case_when(
moon_phase == "New" ~ "π",
moon_phase == "Waxing" ~ "π",
moon_phase == "Full" ~ "π",
moon_phase == "Waning" ~ "π",
TRUE ~ ""))
ggplot(sampling_calendar, aes(x = week_ID, y = zone, fill = sampling, group = sampling_calendar)) +
geom_tile(aes(width = 0.8), color = "white", position = position_dodge(width = 0.9), alpha = 0.5) +
scale_fill_manual(name = "Sample type", values = c(
"No sampling" = "gray90",
"Plankton" = "darkgreen",
"eDNA" = "blue",
"Sediment" = "brown"
)) +
annotate("text", x = moon_phase_labels$week_ID, y = -0.5, label = moon_phase_labels$moon_phase_symbol, size = 3, vjust = -0.5) +
labs(title = "Sampling Calendar", x = "Week Number", y = "Sampling Zones") +
scale_x_continuous(
breaks = seq(min(sampling_calendar$week_ID)-1, max(sampling_calendar$week_ID)-1, by = 4),
sec.axis = sec_axis(~., breaks = date_labels$week_ID, labels = date_labels$date_labels, name = "")
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
})
# Render table with sample totals ####
output$samplingTable <- renderTable({
sampling_calendar <- sampling_calendar_reactive()
if (is.null(sampling_calendar) || nrow(sampling_calendar) == 0) {
return(NULL) # Return nothing if no sampling calendar is available
}
df <- sampling_calendar %>%
select(!starts_with("sampling_flag")) %>%
filter(sampling != "No sampling") %>%
group_by(sampling) %>%
tally(wt = ifelse(zone == "Other zones", input$n_otherzones, 1) * n_replicas)
df
})
}
# Run the application ####
shinyApp(ui = ui, server = server)