-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
executable file
·205 lines (175 loc) · 6.99 KB
/
server.R
File metadata and controls
executable file
·205 lines (175 loc) · 6.99 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
#
# 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/
#
#Load libraries
library(shiny)
library(tidyverse)
library(xts)
library(dygraphs)
library(reshape2)
library(leaflet)
library(broom)
library(lubridate)
library(gridExtra)
library(grid)
#Load cleaned dataset, see "WQ.Storet.Data.Prep.Rmd" for details on where data comes from
#and how it was cleaned.
load('data/ShinyFullDataset.RData')
#Set a ggplot theme for aesthetic purposes
matt_theme <- theme_set(theme_bw())
matt_theme<- theme_update(axis.line = element_line(colour = "black"),
panel.grid.major=element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
text=element_text(family='sans','plain','black',16,0.5,0.5,0,0),
plot.margin=unit(c(6,20,6,2),'pt')
)
# Define server logic to run application
shinyServer(function(input, output) {
#Generate leaflet (interactive map)
output$map <- renderLeaflet({
leaflet() %>%
addPolygons(data=csub,color=csub$color,popup=paste((csub$HU_12_NAME)),group='Catchments') %>%
addMarkers(data=station.sp,popup=paste('Station ID = ',station.sp$Station.ID)) %>%
addCircles(data=min.mine,color='black',weight=7,
popup=paste(min.mine$CFS_DATE, min.mine$PH),group='Mine Outlets') %>%
addProviderTiles('Esri.WorldTopoMap',group='Topo') %>%
addProviderTiles('Esri.WorldImagery',group='Imagery') %>%
addLayersControl(baseGroups=c('Topo','Imagery'),
overlayGroups=c('Catchments','Mine Outlets')
) %>% hideGroup("Mine Outlets")
})
#Generate interactive and zoomable discharge plot for first tab of app
output$q <- renderDygraph({
dygraph(q.xts,group='dy') %>%
dyOptions(colors=c('red','blue'),strokeWidth=1.5) %>%
dyAxis('y',label='Q (cms)')
})
#Generate interactive and zoomable discharge plot for second tab of app
output$q2 <- renderDygraph({
dygraph(q.xts,group='dy') %>%
dyOptions(colors=c('red','blue'),strokeWidth=1.5) %>%
dyAxis('y',label='Q (cms)')
})
#Generate the plot with analyte concentration data.
output$time.chem <- renderDygraph({
d1 <- q.chem %>%
filter(variable == input$analyte) %>%
dcast(.,dateTime~Site,value.var='value',mean)
chem.dy <- xts(d1[,-1],order.by=d1$dateTime) %>%
dygraph(.,group='dy') %>%
dyOptions(colors=c('red','blue'),strokeWidth=0, drawPoints=T,pointSize=4) %>%
dyAxis('y',label='[Analyte] (mg/L)')
if(!is.na(input$thresh)){
chem.dy <- chem.dy %>%
dyLimit(limit=input$thresh,strokePattern='solid',color='green')
}
chem.dy
})
#Generate boxplot and probability density function of data. Dynamic by window set in dygraph group
#Add threshold line capacity
output$diff <- renderPlot({ dts <- numeric()
if(is.null(input$q_date_window)){
dts <- c(min(q.chem$dateTime),max(q.chem$dateTime))
}else{
dts[1] <- (as.Date(input$q_date_window[[1]]))
dts[2] <- (as.Date(input$q_date_window[[2]]))
}
q.diff <- q.chem %>%
filter(!is.na(value)) %>%
filter(variable==input$analyte) %>%
filter(dateTime > dts[1] & dateTime < dts[2])
box <- ggplot(q.diff,aes(x=Site,y=value,group=Site,fill=Site)) +
geom_boxplot(show.legend=F) +
ylab('[Analyte] (mg/L)') +
scale_fill_manual(name='',values=c('red','blue'))
if(!is.na(input$thresh)){
box <- box + geom_hline(yintercept=input$thresh,col='green3',size=1.6)
}
dens <- ggplot(q.diff,aes(value,fill=Site)) +
geom_density(alpha=.7,show.legend = F) +
ylab('Density') +
xlab('[Analyte] (mg/L)') +
scale_fill_manual(name='',values=c('red','blue'))
if(!is.na(input$thresh)){
dens <- dens + geom_vline(xintercept=input$thresh,col='green3',size=1.6)
}
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2,widths=c(0.5,0.5))))
print(box, vp=viewport(layout.pos.row=1,layout.pos.col=1))
print(dens, vp=viewport(layout.pos.row=1,layout.pos.col=2))
})
#Generate plot of Q versus concentration based on dynamic window from dygraph
#Give user a bunch of model options to play with.
output$chemostasis <- renderPlot({
dts <- numeric()
if(is.null(input$q2_date_window)){
dts <- c(min(q.chem$dateTime),max(q.chem$dateTime))
}else{
dts[1] <- (as.Date(input$q2_date_window[[1]]))
dts[2] <- (as.Date(input$q2_date_window[[2]]))
}
q.sub <- q.chem %>%
filter(dateTime > dts[1] & dateTime < dts[2]) %>%
filter(!is.na(value)) %>%
filter(variable == input$analyte1) %>%
filter(value > 0) %>%
mutate(month = month(dateTime))
if(input$season=='all'){
q.sub <- q.sub
}
if(input$season=='summer'){
q.sub <- q.sub %>% filter(month %in% c(5,6,7,8,9,10))
}
if(input$season=='winter'){
q.sub <- q.sub %>% filter(!month %in% c(5,6,7,8,9,10))
}
gplot <- ggplot(q.sub, aes(x=m3s,y=value,color=Site)) +
geom_point(shape=1,size=2) +
geom_point(shape=1,size=2.1) +
scale_color_manual(name='',values=c('red','blue')) +
theme(legend.position=c(.8,.85)) +
xlab('Q (cms)') +
ylab('Analyte Concentration [mg/L]')
if(input$model=='none'){
print(gplot)
}
if(input$model=='yx'){
mod <- lm(value ~ m3s*Site,data=q.sub) %>% glance(.)
print(gplot + stat_smooth(method='lm') +
annotate("text", x=Inf, y = Inf,
label =paste('p = ',round(mod$p.value,3),'R2 =',round(mod$adj.r.squared,2)),
vjust=1.5, hjust=2.8,size=6))
}
if(input$model=='logx'){
mod <- lm(value ~ log10(m3s)*Site,data=q.sub) %>% glance(.)
print(gplot + scale_x_log10() +
annotate("text", x=Inf, y = Inf,
label =paste('p = ',round(mod$p.value,3),'R2 =',round(mod$adj.r.squared,2)),
vjust=1.5, hjust=2.8,size=6) +
stat_smooth(method='lm'))
}
if(input$model=='logy'){
mod <- lm(log10(value) ~ m3s*Site,data=q.sub) %>% glance(.)
print(gplot + scale_y_log10() +
annotate("text", x=Inf, y = Inf,
label =paste('p = ',round(mod$p.value,3),'R2 =',round(mod$adj.r.squared,2)),
vjust=1.5, hjust=2.8,size=6) +
stat_smooth(method='lm'))
}
if(input$model=='logyx'){
mod <- lm(log10(value) ~ log10(m3s)*Site,data=q.sub) %>% glance(.)
print(gplot + scale_y_log10() +
scale_x_log10() +
annotate("text", x=Inf, y = Inf,
label = paste('p = ',round(mod$p.value,3),'R2 =',round(mod$adj.r.squared,2)),
vjust=1.5, hjust=2.8,size=6) +
stat_smooth(method='lm'))
}
})
})