-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_soil_water.R
238 lines (196 loc) · 6.48 KB
/
module_soil_water.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
# get soil water summary for selected sites
water_summary_extractor_SQL <- function(fields = "") {
if (!length(fields) || any(nchar(fields) == 0)) { return(NULL) }
con <- make_con()
on.exit(dbDisconnect(con))
fieldlist <- stringr::str_c("\'", fields, "\'", collapse = ",")
install_queries <- purrr::map(
c("bare_node_serial_no", "cover_node_serial_no"),
~glue::glue(
'
SELECT
"code", "subplot", "serial",
\'{substr(.x, 1, 1)}\' AS "trt",
"time_begin",
COALESCE("time_end", "possible_time_end", "end_of_year") AS "time_end"
FROM (
SELECT
"code", "subplot", "serial",
"time_begin", "time_end", "possible_time_end",
CAST(CONCAT(EXTRACT(year FROM "time_begin"), \'-11-01 12:00:00\') AS TIMESTAMP) AS "end_of_year"
FROM (
SELECT
"code", "subplot", "serial",
"time_begin", "time_end",
LEAD("time_begin", 1, NULL) OVER (PARTITION BY "serial" ORDER BY "code", "time_begin") AS "possible_time_end"
FROM (
SELECT
"code", "subplot",
"{.x}" AS "serial",
"time_begin", "time_end"
FROM "wsensor_install"
WHERE ("code" IN ({fieldlist}))
ORDER BY "code", "time_begin"
) AS filtered_subquery
) AS filled_subquery
) AS guessed_subquery
'
)
)
purrr::map(
install_queries,
~tbl(con, sql(.x))
) %>%
{union_all(.[[1]], .[[2]])} %>%
compute("temp_installs")
subsets <- tbl(con, sql(
'
SELECT * FROM water_sensor_data
LEFT JOIN temp_installs
ON (
"timestamp" > time_begin
AND "timestamp" < time_end
AND node_serial_no = serial
)
'
)
)
subsets %>%
filter(!is.na(code)) %>%
mutate(d = as.Date(timestamp)) %>%
filter(between(vwc, 0, 100)) %>%
group_by(code, trt, d) %>%
summarise(
vwc = mean(vwc, na.rm = T)
) %>%
mutate(
inches = vwc / 2.54
) %>%
arrange(d) %>%
ungroup() %>%
mutate(d = as_datetime(d)) %>%
collect()
}
# generate box elements
water_boxer <- function(input, output, session, inputcode, data) {
if (is.null(data)) return(NULL)
data_at_inputcode <- data %>% filter(code == inputcode)
prettyid_at_inputcode <- na.omit(data_at_inputcode$prettyid[1])
pal <- scales::hue_pal(h.start = 30, l = 60)(3)[1:2]
output$showWaterBox <- reactive(nrow(data_at_inputcode))
outputOptions(output, "showWaterBox", suspendWhenHidden = FALSE)
output$surplus <- renderPlot({
data_wide <- data_at_inputcode %>%
select(-vwc) %>%
spread(key = trt, value = inches) %>%
mutate(surplus = gracefully(c-b)) %>%
filter(!is.na(surplus))
rng_max <- max(abs(range(data_wide$surplus, na.rm = T)))
if (
!is.finite(rng_max) |
nrow(data_at_inputcode) == 0 |
nrow(data_wide) == 0 |
length(unique(data_at_inputcode$trt)) != 2
) {
output$errsurplus <- renderText("Some data is missing. Check back soon.")
return(NULL)
}
data_wide %>%
ggplot(aes(d, surplus)) +
geom_col(
aes(fill = factor(surplus>=0)),
show.legend = F,
width = 60*60*24,
na.rm = T
) +
scale_y_continuous(
"Saved water, inches", labels = abs,
limits = c(-1,1)*rng_max,
sec.axis = dup_axis(name = " Cover crop No cover ")) + # ← →
scale_fill_manual(values = set_names(pal, c("FALSE", "TRUE"))) +
theme_minimal() +
labs(x = NULL) +
theme(axis.title = element_text(size = rel(1.2)),
axis.text = element_text(size = rel(1.2)))
})
output$fieldcap <- renderPlot({
if (nrow(data_at_inputcode) == 0) {
output$errfieldcap <- renderText("Some data is missing. Check back soon.")
return(NULL)
}
data_at_inputcode %>% group_by(code, trt, prettyid) %>%
mutate(count_days_backwards = row_number(-as.numeric(d))) %>%
ungroup() %>%
#filter(count_days_backwards < 7) %>%
group_by(code, trt, prettyid) %>%
summarise(pctoffc = mean(inches, na.rm = T)/(3.5*3.28)) %>%
ggplot(aes(0, pctoffc, fill = trt)) +
geom_col(position = position_dodge(width = 1), show.legend = F, na.rm = T) +
geom_hline(yintercept = 0:1, size = 1) +
scale_y_continuous(
"% of soil water field capacity",
breaks = (0:4)/4,
labels = scales::percent
) +
scale_x_continuous(NULL, breaks = NULL) +
scale_fill_manual(values = set_names(pal, c("b", "c"))) +
theme_minimal() +
theme(
axis.text = element_text(size = rel(1.2)),
axis.title = element_text(size = rel(1.2)),
axis.text.y = element_blank()
) +
coord_flip()
})
output$waterhead <- renderUI({
tags$strong(glue::glue("{prettyid_at_inputcode} ({data_at_inputcode$year[1]})"))
})
output$watertext <- renderUI({
if (nrow(data_at_inputcode) == 0) return(NULL)
div(
"In both these graphs",
span(tags$strong("green"), style = glue::glue("color:{pal[2]};")),
"refers to the cover crop, and",
span(tags$strong("orange-brown"), style = glue::glue("color:{pal[1]};")),
"refers to the bare ground.",
tags$strong("Field capacity"),
"is used here to mean the approximate amount of",
"water your soil can hold (without being saturated) in the top 3 feet.",
"This graph is averaged over time, representing the whole season.",
tags$strong("Saved water"),
"is how many more inches of water (in the top 3 feet)",
"one plot had relative to the other each day."
)
})
}
# layout box: don't forget ns() inside *Output(...)
water_boxerUI <- function(id) {
ns <- NS(as.character(id))
conditionalPanel(
"output.showWaterBox",
div(
div(
htmlOutput(ns("waterhead")),
class = "card-header"
),
div(
fluidRow(
column(
5, textOutput(ns("errfieldcap")),
plotOutput(ns("fieldcap"), height = "20vh") %>%
shinycssloaders::withSpinner(type = 6, color = "#158cba"),
tags$br(),
htmlOutput(ns("watertext"))
),
column(
7, textOutput(ns("errsurplus")),
plotOutput(ns("surplus"), height = "35vh")
)
),
class = "card-body"
) ,
class = "card border-primary mb-3"
),
ns = ns
)
}