-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
318 lines (260 loc) · 9.12 KB
/
server.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
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
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
function(input, output) {
# Read and reformat uploaded psy data
psy <- reactive({
req(input$upload_psy)
inFile <- input$upload_psy
initial <- read_csv(inFile$datapath) # "data_examples/psy_2a.csv"
# find date and time - create dt
if("dt" %nin% colnames(initial)) {
d_name <- initial %>%
select(where(is.Date)) %>%
colnames()
t_name <- initial %>%
select(where(hms::is_hms)) %>%
colnames()
initial %>%
mutate( # WIP to figure out adding on-the-fly conversion of data
# dt = as.POSIXct(paste0(!!sym(d_name), !!sym(t_name))),
month = lubridate::month(dt))
} else {
initial %>%
mutate(month = lubridate::month(dt),
date = as.Date(dt))
}
})
# Read and reformat uploaded env data
env <- reactive({
req(input$upload_env)
inFile <- input$upload_env
initial <- read_csv(inFile$datapath) # "data_examples/neon_vpd30.csv"
initial
})
# Dynamic selectInput for month
output$dyn_month <- renderUI({
req(input$upload_psy)
selectInput("month", "Select month:",
unique(psy()$month))
})
# Dynamic selectInput for PSY, psy data
output$dyn_psyvar <- renderUI({
req(input$upload_psy)
# only provide POSIXct objects
temp <- psy() %>%
select(where(is.numeric))
varSelectInput("psyvar", "Select WP variable:",
temp)
})
# Dynamic selectInput for x variable, env data
output$dyn_xvar <- renderUI({
req(input$upload_env)
# only provide POSIXct objects
temp <- env() %>%
select(where(is.POSIXct))
varSelectInput("xvar", "Select x variable:",
temp)
})
# Dynamic selectInput for y variable, 1st axis, env data
output$dyn_yvar1 <- renderUI({
req(input$upload_env)
# only provide numeric objects
temp <- env() %>%
select(where(is.numeric))
varSelectInput("yvar1", "Select y variable (left axis):",
temp)
})
# Dynamic selectInput for y variable, 2nd axis, env data
output$dyn_yvar2 <- renderUI({
req(input$upload_env)
# only provide numeric objects
temp <- env() %>%
select(where(is.numeric))
varSelectInput("yvar2", "Select y variable (right axis):",
temp)
})
# Dynamically updated slider input based on month selected
output$dyn_range <- renderUI({
req(input$month)
temp <- psy() %>%
filter(month == input$month) %>%
pull(date) %>%
range()
sliderInput("daterange", "Select date range:",
min = temp[1],
max = temp[2],
value = temp,
width = '93%',
ticks = FALSE)
})
# Initial selection of no points
selected <- reactiveVal()
# Create vector of FAlSE the length of the input psychrometer timeseries
observeEvent(input$upload_psy, {
selected(rep(FALSE, nrow(psy())))
})
# Create observed points within brush area
observeEvent(input$plot1_brush, {
brushed <- brushedPoints(psy(), input$plot1_brush, allRows = TRUE)$selected_
selected(brushed | selected())
})
# Create observed points with click
observeEvent(input$plot1_click, {
clicked <- nearPoints(psy(), input$plot1_click, allRows = TRUE)$selected_
selected(clicked | selected())
})
# Reset point if double clicked
observeEvent(input$plot_reset, {
dblclicked <- nearPoints(psy(), input$plot_reset, allRow = TRUE)$selected_
temp <- selected()
ind <- which(dblclicked == TRUE)
temp[ind] <- FALSE
selected(temp)
})
# ggplot timeseries of psy data
output$psy_plot <- renderPlot({
req(input$upload_psy)
req(input$daterange)
# Update with selected
psy_temp <- psy() %>%
mutate(sel = selected())
# custom integer labels
# int_breaks <- function(x, n = 5) {
# l <- pretty(x, n)
# l[abs(l %% 1) < .Machine$double.eps ^ 0.5]
# }
# Filter and plot
psy_temp %>%
filter(month == input$month) %>%
filter(date >= input$daterange[1],
date <= input$daterange[2]) %>%
ggplot() +
geom_point(aes(x = dt, y = !!input$psyvar,
color = sel)) +
scale_y_continuous("Water potential") +
# , breaks = int_breaks) +
scale_x_datetime(limits = c(as.POSIXct(paste(input$daterange[1], "00:00")),
as.POSIXct(paste(input$daterange[2], "00:00"))),
date_breaks = "2 days",
date_labels = "%d") +
scale_colour_manual(limits = c("TRUE", "FALSE"),
values = c( "#CC6677", "#117733")) +
theme_bw(base_size = 16) +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold")) +
guides(color = "none")
})
# ggplot timeseries of env data
# require 2 inputs and the x variable as POSIX object
output$env_plot <- renderPlot({
req(input$upload_env)
req(input$daterange)
req(input$xvar, input$yvar1, input$yvar2)
temp <- env() %>%
filter(!!input$xvar >= input$daterange[1],
!!input$xvar <= input$daterange[2])
# create character strings of selected columns
lab_left <- as.character(input$yvar1)
lab_right <- as.character(input$yvar2)
# create ratio for scaling left and right axes
ratio1 <- if ( max(temp[[lab_left]], na.rm = TRUE) > max(temp[[lab_right]], na.rm = TRUE)) {
max(max(temp[[lab_left]], na.rm = TRUE),
max(temp[[lab_right]], na.rm = TRUE)) /
min(max(temp[[lab_left]], na.rm = TRUE),
max(temp[[lab_right]], na.rm = TRUE))
} else {
min(max(temp[[lab_left]], na.rm = TRUE),
max(temp[[lab_right]], na.rm = TRUE)) /
max(max(temp[[lab_left]], na.rm = TRUE),
max(temp[[lab_right]], na.rm = TRUE))
}
print(max(temp[[lab_left]], na.rm = TRUE) > max(temp[[lab_right]], na.rm = TRUE))
print(ratio1)
# For tuning the relative sizes of left and right y axes
# ratio1 <- if ( max(temp$VPD, na.rm = TRUE) > max(temp$p34_6, na.rm = TRUE) ) {
# max(max(temp$VPD, na.rm = TRUE),
# max(temp$p34_6, na.rm = TRUE),
# na.rm = TRUE) /
# max(temp$p34_6, na.rm = TRUE)
# } else {
# max(temp$p34_6, na.rm = TRUE) /
# max(max(temp$VPD, na.rm = TRUE),
# max(temp$p34_6, na.rm = TRUE),
# na.rm = TRUE)
# }
# Name color vector
color_vec <- c("#6699CC","#44AA99")
names(color_vec) <- c(lab_left, lab_right)
if(is.infinite(ratio1) | is.na(ratio1) | is.nan(ratio1) | ratio1 == 0) {
ggplot() +
theme_void() +
geom_text(aes(0, 0, label = 'One or more covariates is missing for this time period'))
} else {
temp %>%
ggplot() +
geom_point(aes(x = !!input$xvar,
y = !!input$yvar1,
color = lab_left)) +
geom_point(aes(x = !!input$xvar,
y = !!input$yvar2*ratio1,
color = lab_right)) +
scale_y_continuous(lab_left,
sec.axis = sec_axis(~./ratio1,
name = lab_right)) +
scale_x_datetime(date_breaks = "2 days",
date_labels = "%d") +
theme_bw(base_size = 16) +
scale_color_manual(values = color_vec) +
guides(color = "none") +
theme(axis.title.y.left = element_text(color = "#6699CC", face = "bold"),
axis.title.y.right = element_text(color = "#44AA99", face = "bold"),
axis.title.x = element_blank())
}
})
# Sidebar table of data to remove
output$brush_info_remove <- renderPrint({
temp <- psy() %>%
mutate(to_remove = selected()) %>%
filter(to_remove == TRUE) %>%
select(date, corrected_water_potential_m_pa) %>%
rename(corrected_WP = corrected_water_potential_m_pa)
return(print(temp, n = 1500))
})
# report all data
clean_all <- reactive({
psy_all <- psy() %>%
mutate(to_remove = selected())
return(psy_all)
})
# report month data
clean_month <- reactive({
psy_month <- psy() %>%
mutate(to_remove = selected()) %>%
filter(month == input$month)
return(psy_month)
})
# Button to download cleaned month
output$download_clean_month <- downloadHandler(
filename = function() {
glue::glue("clean-{input$month}-{input$upload_psy}")
},
content = function(file) {
write_csv(x = clean_month(), file = file)
}
)
# Button to download cleaned all
output$download_clean_all <- downloadHandler(
filename = function() {
glue::glue("clean-{input$upload_psy}")
},
content = function(file) {
write_csv(clean_all(), file)
}
)
}