-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.R
142 lines (103 loc) · 4.85 KB
/
App.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
library(shiny)
library(caret)
library(doMC)
library(here)
source(here::here('libraries.R'))
# Import Model data -------------------------------------------------------
Obs_Data<- list.files(here::here('Data/Parques/Belesar/Historico/WEB/PM/'), full.names = T) %>%
.[str_detect(.,"Obs_")] %>% .[which.max( str_split(., "/") %>% lapply(., function(x) x[length(x)])%>% str_remove("Obs_|.RDS") %>%
str_replace("--"," ") %>% ymd_hms())] %>% readRDS()
WRF_data<- list.files(here::here('Data/Parques/Belesar/Historico/WEB/PM/'), full.names = T) %>%
.[str_detect(.,"WRF_")] %>% .[which.max( str_split(., "/") %>% lapply(., function(x) x[length(x)])%>% str_remove("WRF_|.RDS") %>%
str_replace("--"," ") %>% ymd_hms())] %>% readRDS()
Tabla_1<- Obs_Data %>% mutate(diff_nivel=c(NA,diff(nivel)))
Tabla_1<- left_join(Tabla_1, WRF_data[[23]]$D1[,c("Date", "prep_hourly")], by="Date")
# Predecir aportacion a través de differencia de nivel --------------------
Tabla_DN_IF<- Tabla_1
Tabla_DN_IF[,c("Vol","Temp", "porcentaje","prep_hourly")]<- NULL
Tabla_DN_IF<- Tabla_DN_IF[complete.cases(Tabla_DN_IF),]
Tabla_DN_IF$aport_SMA<- SMA(Tabla_DN_IF$aport, 12)
Tabla_DN_IF$difnivel_SMA<- SMA(Tabla_DN_IF$diff_nivel, 12)
Tabla_DN_IF<- Tabla_DN_IF[complete.cases(Tabla_DN_IF),]
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Belesar Predictive model"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Elegimos que gráficas queremos mostrar ---------------------------------
checkboxGroupInput("checkGroup", label = h3("Gráficas"),
choices = list("Prediccion de diff nivel" = 1,
"Prediccion inflow" = 2,
"Ambas" = 3),
selected = NULL),
# Elegimos metodo y Tunelength para inflow predicction -------------------------------------------------
selectInput(inputId = "Method_inflow",
label = "Choose method for predict inflow from diff nivel:",
choices = c("svmLinear", "lm", "other")),
# Input: Selecciona parámetro tuneLenght
numericInput(inputId = "TuneLength1",
label = "Number of tune values:",
value = 10),
# Elegimos metodo y tuneLength para diff nivel prediction -----------------
selectInput(inputId = "Method_diff",
label = "Choose method for predict diff nivel from WRF rain:",
choices = c("svmLinear", "lm", "other")),
# Input: Selecciona parámetro tuneLenght
numericInput(inputId = "TuneLength2",
label = "Number of tune values:",
value = 10),
# Elegimos periodo de predicción -----------------------------------------
dateRangeInput("Date_prediccion", h3("Rango de predicción"), start =Tabla_DN_IF)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
#Cortamos los datos en Entrenamiento y predicción
T_DN_IF_data<- Tabla_DN_IF[Tabla_DN_IF$Date< ymd("2019/01/25"), ]
P_DN_IF_data<- Tabla_DN_IF[Tabla_DN_IF$Date> ymd("2019/01/25"), ]
# Return the requested inflow method ----
inflow_method <- reactive({
switch(input$Model_inflow,
"svmLinear" = "svmLinear",
"lm" = "lm",
"other" = "other")
})
modelo_DN_AP<- train(aport_SMA ~ difnivel_SMA,
data=T_DN_IF_data,
method=inflow_method,
tuneLength=input$TuneLength)
ggplot(data = P_DN_IF_data)+
geom_line(aes(y=P_DN_IF_data$aport,
x=P_DN_IF_data$Date),
alpha=0.5)+
geom_line(aes(y=P_DN_IF_data$aport_SMA,
x=P_DN_IF_data$Date),
alpha=0.8)+
ylab("Aportacion [m³/s]")+
xlab(paste(range(P_DN_IF_data$Date), collapse = "\n"))+
geom_line(aes(y=predict(modelo, newdata= P_DN_IF_data),
x=Date),
col="red", lty=2)+
theme_light()
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)