-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshiny_small.Rmd
180 lines (154 loc) · 4.92 KB
/
shiny_small.Rmd
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
---
title: "Attempt 1"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(ggplot2)
library(dplyr)
library(tidyr)
library(magrittr)
library(shinythemes)
library(zoo)
library(tidyverse)
```
Load Data for one meter
```{r}
load("../smalldata.RData")
load("../smalljobdata.RData")
#datFlow$date = as.Date(datFlow$date)
```
Define UI
```{r}
ui = fluidPage(theme = shinytheme("lumen"),
titlePanel("Time series for given meter ID"),
sidebarLayout(
sidebarPanel(
# select meter id to be plotted
selectInput(inputId = "meter_id",
label = strong("Meter Id"),
choices = unique(datFlow$id),
selected = "AW008"),
dateRangeInput(inputId = "date",
strong("Date range"),
start = "2017-01-01",
end = "2020-01-26",
min = "2017-01-01",
max = "2020-01-26"),
sliderInput("roll_mean_days",
"Rolling Mean Window Size:",
min = 1, max = 30,
value = 7),
),
# output
mainPanel(
plotOutput(outputId = "timeseries")
)
)
)
```
Define Server function
```{r}
server = function(input, output) {
selected_series = reactive({
# validate input data
req(input$date)
validate(need(!is.na(input$date[1]) & !is.na(input$date[2]),
"Error: Please provide both a start and an end date."))
validate(need(input$date[1] < input$date[2],
"Error: Start date should be earlier than end date."))
# filter data by given meter id and date range
datFlow %>%
filter(id == input$meter_id) %>%
filter(date >= as.Date(input$date[1]) & date <= as.Date(input$date[2])
)
})
# night start % night end
night_start = 9 # 2 am
night_end = 13 # 3 am
# Compute MNF and ADF
mnf = reactive({
selected_series() %>%
filter((night_start <= tod) & (tod <= night_end)) %>%
group_by(id,date) %>%
summarise(value=min(y)) %>%
ungroup()
})
adf = reactive({
selected_series() %>%
filter((night_start > tod) | (tod > night_end)) %>%
group_by(id, date) %>%
summarise(value=mean(y)) %>%
ungroup()
})
# function to compute the rolling mean and uses interpolation to impute NAs
inter_roll <- function(df, n_days=7){
new <- df %>%
mutate(value_roll=rollmean(value, n_days, na.pad=TRUE)) %>%
select(-value)
new$value_roll = na.spline(new$value_roll, 1:nrow(new))
return(new)
}
# get rolling means for mnf and adf
mnf_roll = reactive({
inter_roll(mnf(), input$roll_mean_days)
})
adf_roll = reactive({
inter_roll(adf(), input$roll_mean_days)
})
# Put mnf and adf together
mnf_adf = reactive({
mnf_roll() %>%
left_join(adf_roll(), by=c("id","date"), suffix=c("_mnf", "_adf")) %>%
transform(difference=value_roll_adf - value_roll_mnf)
})
# plot(diff(mnf_adf$difference, lag=1), type='l')
# Put into long format
mnf_adf_1 = reactive({
mnf_adf() %>%
gather("key", "y", -id, -date)
})
# All repair jobs
alljobs = repFlow %>%
select(-Region, -Area, -Area_reference) %>%
gather("key", "value", -id) %>%
na.omit
# start of jobs
job_starts = reactive({
alljobs %>%
filter(id == input$meter_id) %>%
filter(str_detect(key, "Start")) %>%
filter(as.Date(value) >= as.Date(input$date[1]), as.Date(value) <= as.Date(input$date[2]))
})
# # end of jobs
job_ends = reactive({
alljobs %>%
filter(id == input$meter_id) %>%
filter(str_detect(key, "End")) %>%
filter(value >= as.Date(input$date[1]), value <= as.Date(input$date[2]))
})
# create plot object that the plotOutput function is expecting
output$timeseries = renderPlot({
plot = ggplot(mnf_adf_1(),aes(x=as.Date(date), y=y, color=key)) +
geom_line() +
theme_bw() +
labs(x="Date", y="Flow")
if(nrow(job_starts())){
plot <- plot +
geom_vline(xintercept = as.Date(job_starts()$value), lty=2)
}
if(nrow(job_ends())){
plot <- plot + geom_vline(xintercept = as.Date(job_ends()$value), lty=1)
}
plot
})
output$test = renderPlot({
ggplot(mnf_adf_1(), aes(x = as.Date(date), y = y, color = key)) +
geom_line()
})
}
# create shiny object
shinyApp(ui = ui, server = server)
```