Commit 6d6a9c17 authored by Matteo DE FELICE's avatar Matteo DE FELICE

Initial commit

parents
shinyServer(function(input, output, session) {
#
# # Combine the selected variables into a new data frame
# selectedData <- reactive({
#
# if (input$timescale == 'Yearly') {
# dd_m = dd_m %>% group_by(Date = year(Date), country, type) %>%
# dplyr::summarise(value = mean(value, na.rm = T))
# dd_m$Date = as.Date(as.yearmon(dd_m$Date))
# } else if (input$timescale == 'JFM') {
# dd_m = dd_m %>% dplyr::filter(month(Date) < 4) %>%
# group_by(Date = year(Date), country, type) %>%
# dplyr::summarise(value = mean(value, na.rm = T))
# dd_m$Date = as.Date(as.yearmon(dd_m$Date))
# } else if (input$timescale == 'JJA') {
# dd_m = dd_m %>% dplyr::filter(month(Date) >= 6 & month(Date) <= 8) %>%
# group_by(Date = year(Date), country, type) %>%
# dplyr::summarise(value = mean(value, na.rm = T))
# dd_m$Date = as.Date(as.yearmon(dd_m$Date))
# } else {
# dd_m$Date = as.Date(dd_m$Date)
# }
# if (input$norm) {
# dd_m = dd_m %>% group_by(country, type) %>%
# dplyr::mutate(value = (value - mean(value))/ sd(value))
# }
# dplyr::filter(dd_m, country %in% input$country,
# type %in% input$var, year(Date) %in% seq(input$years[1],
# input$years[2]))
#
# })
#
# selectedPCA <- reactive({
# if (input$PCvar == 't2m') {
# this_env = t2m_env
# } else {
# this_env = pp_env
# }
#
# if (input$timescale == 'Yearly') {
# selection = this_env$p_yrl
# timeindex = this_env$ti_yrl
# } else if (input$timescale == 'JFM') {
# selection = this_env$p_jfm
# timeindex = this_env$ti_jfm
# } else if (input$timescale == 'JJA') {
# selection = this_env$p_jja
# timeindex = this_env$ti_jja
# } else {
# selection = this_env$p_mon
# timeindex = this_env$ti_mon
# }
# selection$timeindex = timeindex
# selection
# })
#
output$zone_selection <- renderUI({
zones = readxl::read_xlsx(paste0('xls/', input$result_xls), sheet = 'LMP') %>%
names()
selectInput('zone', 'Choose the zone:',
choices = zones[-1])
})
output$main_plot <- renderPlot({
# TODO Date is variable -> especially length out
res = readxl::read_xlsx(paste0('xls/', input$result_xls), sheet = 'Reservoirs') %>%
mutate(date = seq(as_datetime('2016-01-01'), as_datetime('2016-12-31'), length.out = 366))
hrgen = readxl::read_xlsx(paste0('xls/', input$result_xls), sheet = 'Hydro power generation') %>%
mutate(date = seq(as_datetime('2016-01-01'), as_datetime('2016-12-31'), length.out = 366))
gen = readxl::read_xlsx(paste0('xls/', input$result_xls), sheet = 'Generation') %>%
mutate(date = seq(as_datetime('2016-01-01'), as_datetime('2016-12-31'), length.out = 366))
wv = readxl::read_xlsx(paste0('xls/', input$result_xls), sheet = 'Water value') %>%
mutate(date = seq(as_datetime('2016-01-01'), as_datetime('2016-12-31'), length.out = 366))
# Average water value ---------------------------------------------------------------------
if (input$plot_type == 'awv') {
sel <-
wv %>%
select(-X__1 ) %>%
gather(source, value, -date) %>%
group_by(source) %>%
summarise(avg_cost = mean(value)) %>%
filter(avg_cost <= input$num_maxhwm)
g = ggplot(sel, aes(x = fct_reorder(source, avg_cost), y = avg_cost)) +
geom_bar(stat = 'identity', fill = 'lightblue', color = 'grey66') +
{if (input$check_logscale) scale_y_log10(breaks = c(0.1, 1, 10, 100, 1000, 10000, 100000)) }+
{if (input$check_logscale) ylab('Water value (log10)')} +
{if (!input$check_logscale) ylab('Water value ')} +
xlab('Zone') +
theme_minimal() +
theme(axis.text.x = element_text(size = 8, angle = 90, hjust = 1))
print(g)
}
# Heatmap water value ----------------------------------------------------------------------
else if (input$plot_type == 'hwv') {
sel <-
wv %>%
select(-X__1 ) %>%
gather(source, value, -date) %>%
group_by(source) %>%
mutate(avg_cost = mean(value)) %>%
filter(avg_cost < 100)
g = ggplot(sel,
aes(x = date, y = fct_reorder(source, avg_cost),
fill = cut(value, right = FALSE, breaks = c(0, seq(10, 50, 5))))) +
geom_tile() +
scale_fill_brewer(palette = 'YlOrRd', name = 'water value') +
ylab('Zone') +
theme_minimal()
print(g)
}
# Generation mix ----------------------------------------------------------------------------
else if (input$plot_type == 'gen_mix') {
sel_zone = select(gen, date, contains(paste0(input$zone, '_')))
print(names(sel_zone))
color_palette = c('HPHS' = 'blue4',
'HROR' = 'blue2',
'PHOT' = 'yellow',
'STUR' = 'brown4',
'GTUR' = 'brown2',
'WTON' = 'chartreuse2',
'WTOF' = 'chartreuse3',
'BIO' = 'darkolivegreen1',
'COMC' = 'brown')
g = sel_zone %>%
gather(source, generation, -date) %>%
mutate(source_type = str_split(source, '_', simplify = TRUE)[,2]) %>%
group_by(date, source_type) %>%
summarise(generation = sum(generation)) %>%
group_by(source_type) %>%
mutate(variance = sd(generation)/mean(generation)) %>%
ungroup() %>%
mutate(source_type = fct_reorder(source_type, -variance)) %>%
ggplot(aes(x = date, y = generation, group = source_type,
fill = source_type)) +
scale_fill_manual(values = color_palette) +
geom_area()
print(g)
}
# Reservoir levels all ------------------------------------------------------------------
else if (input$plot_type == 'res_all') {
res_data = res %>% gather(zone, reservoir, -date, -X__1) %>%
select(-X__1)
g = ggplot(res_data, aes(x = date, y = reservoir, color = zone, group = zone)) +
geom_line(size = 0.5) +
scale_y_log10() +
ylab('GWh/day') +
theme(axis.text.x = element_text(size = 4, angle = 45, hjust = 1))
print(g)
}
})
})
\ No newline at end of file
library(shiny)
library(tidyverse)
library(lubridate)
shinyUI( fluidPage(
# Application title
titlePanel("Dispa-SET MTHC Results analysis"),
# Sidebar
sidebarLayout(
sidebarPanel(
selectInput("result_xls", "Choose a result file:",
choices = list.files('xls/', pattern = 'xlsx')),
selectInput('plot_type', 'Choose the plot type:',
choices = c('average water value' = 'awv',
'heatmap water value' = 'hwv',
'reservoir levels (all)' = 'res_all',
'reservoir levels (single)' = 'res_single',
'generation mix' = 'gen_mix')),
conditionalPanel(
condition = "input.plot_type == 'gen_mix'",
uiOutput("zone_selection")
),
conditionalPanel(
condition = "input.plot_type == 'awv'",
checkboxInput("check_logscale", label = "Log10 y-axis", value = TRUE),
numericInput("num_maxhwm", label = h3("Max shown value"), value = 1000)
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("main_plot", height = "600px")
)
)
))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment