-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_line_chart.R
151 lines (124 loc) · 6.06 KB
/
app_line_chart.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
# Instala y carga los paquetes necesarios
if (!require("shiny")) install.packages("shiny")
if (!require("readr")) install.packages("readr")
if (!require("plotly")) install.packages("plotly")
library(shiny)
library(readr)
library(plotly)
if(!exists("gas_station_data_loaded")){
source("GasStation_dataset_load.R")
}
ruta_del_archivo1 <- "./CSV_Log/preciosEESS_es_19_Sept.csv"
datosSept <- read.csv(ruta_del_archivo1)
ruta_del_archivo2 <- "./CSV_Log/preciosEESS_es_19_Oct.csv"
datosOct <- read.csv(ruta_del_archivo2)
ruta_del_archivo3 <- "./CSV_Log/preciosEESS_es_19_Nov.csv"
datosNov <- read.csv(ruta_del_archivo3)
ruta_del_archivo4 <- "./CSV_Log/preciosEESS_es_19_Dic.csv"
datosDic <- read.csv(ruta_del_archivo4)
# Define la interfaz de usuario
ui <- fluidPage(
titlePanel("Price over the time"),
sidebarLayout(
sidebarPanel(
selectInput("start_month", "Start month:", choices = c("September", "October", "November", "December"), selected = "September"),
uiOutput("end_month_ui"), # Aquí se renderizará el selector de final
),
mainPanel(
tags$head(
tags$style(
"#lineChart {
border-radius: 15px;
height: 80vh;
}"
)
),
plotlyOutput("lineChart")
)
)
)
server <- function(input, output) {
output$end_month_ui <- renderUI({
req(input$start_month) # Asegúrate de que start_month está disponible
months <- c("September", "October", "November", "December")
start_index <- match(input$start_month, months)
end_index <- if (!is.null(input$end_month)) match(input$end_month, months) else length(months)
choices <- months[start_index:length(months)]
if (start_index > end_index) {
selectInput("end_month", "End month:", choices = choices, selected = "December")
} else {
selectInput("end_month", "End month:", choices = choices, selected = ifelse(is.null(input$end_month), "December", input$end_month))
}
})
# Calcula las medias por tipo de combustible y mes para octubre
medias_por_tipo_oct <- reactive({
mean_95_E5_auto <- mean(datosOct$`Precio.gasolina.95.E5`, na.rm = TRUE)
mean_98_E5_auto <- mean(datosOct$`Precio.gasolina.98.E5`, na.rm = TRUE)
mean_gasoleo_A_auto <- mean(datosOct$`Precio.gasóleo.A`, na.rm = TRUE)
mean_gasoleo_Premium_auto <- mean(datosOct$`Precio.gasóleo.Premium`, na.rm = TRUE)
data.frame(
Mes = factor("October", levels = c("September", "October", "November", "December")),
Combustible = c("Gasoline 95 E5", "Gasoline 98 E5", "Diesel A", "Diesel A Premium"),
Media = c(mean_95_E5_auto, mean_98_E5_auto, mean_gasoleo_A_auto, mean_gasoleo_Premium_auto)
)
})
# Calcula las medias por tipo de combustible y mes para septiembre
medias_por_tipo_sept <- reactive({
mean_95_E5_auto <- mean(datosSept$`Precio.gasolina.95.E5`, na.rm = TRUE)
mean_98_E5_auto <- mean(datosSept$`Precio.gasolina.98.E5`, na.rm = TRUE)
mean_gasoleo_A_auto <- mean(datosSept$`Precio.gasóleo.A`, na.rm = TRUE)
mean_gasoleo_Premium_auto <- mean(datosSept$`Precio.gasóleo.Premium`, na.rm = TRUE)
data.frame(
Mes = factor("September", levels = c("September", "October", "November", "December")),
Combustible = c("Gasoline 95 E5", "Gasoline 98 E5", "Diesel A", "Diesel A Premium"),
Media = c(mean_95_E5_auto, mean_98_E5_auto, mean_gasoleo_A_auto, mean_gasoleo_Premium_auto)
)
})
# Calcula las medias por tipo de combustible y mes para noviembre
medias_por_tipo_nov <- reactive({
mean_95_E5_auto <- mean(datosNov$`Precio.gasolina.95.E5`, na.rm = TRUE)
mean_98_E5_auto <- mean(datosNov$`Precio.gasolina.98.E5`, na.rm = TRUE)
mean_gasoleo_A_auto <- mean(datosNov$`Precio.gasóleo.A`, na.rm = TRUE)
mean_gasoleo_Premium_auto <- mean(datosNov$`Precio.gasóleo.Premium`, na.rm = TRUE)
data.frame(
Mes = factor("November", levels = c("September", "October", "November", "December")),
Combustible = c("Gasoline 95 E5", "Gasoline 98 E5", "Diesel A", "Diesel A Premium"),
Media = c(mean_95_E5_auto, mean_98_E5_auto, mean_gasoleo_A_auto, mean_gasoleo_Premium_auto)
)
})
# Calcula las medias por tipo de combustible y mes para diciembre
medias_por_tipo_dic <- reactive({
mean_95_E5_auto <- mean(datosDic$`Precio.gasolina.95.E5`, na.rm = TRUE)
mean_98_E5_auto <- mean(datosDic$`Precio.gasolina.98.E5`, na.rm = TRUE)
mean_gasoleo_A_auto <- mean(datosDic$`Precio.gasóleo.A`, na.rm = TRUE)
mean_gasoleo_Premium_auto <- mean(datosDic$`Precio.gasóleo.Premium`, na.rm = TRUE)
data.frame(
Mes = factor("December", levels = c("September", "October", "November", "December")),
Combustible = c("Gasoline 95 E5", "Gasoline 98 E5", "Diesel A", "Diesel A Premium"),
Media = c(mean_95_E5_auto, mean_98_E5_auto, mean_gasoleo_A_auto, mean_gasoleo_Premium_auto)
)
})
# Combina todos los data frames en uno solo
medias_combinadas <- reactive({
req(input$start_month, input$end_month) # Asegúrate de que start_month y end_month están disponibles
datos <- list("September" = medias_por_tipo_sept(), "October" = medias_por_tipo_oct(), "November" = medias_por_tipo_nov(), "December" = medias_por_tipo_dic())
meses <- match(c(input$start_month, input$end_month), c("September", "October", "November", "December"))
if (any(is.na(meses))) {
return(NULL) # Devuelve NULL si start_month o end_month no son válidos
}
do.call(rbind, datos[meses[1]:meses[2]])
})
# Envía el gráfico de puntos al UI
output$lineChart <- renderPlotly({
medias_combinadas_data <- medias_combinadas()
plot_ly(data = medias_combinadas_data, x = ~Mes, y = ~Media, color = ~Combustible, type = 'scatter', mode = 'lines+markers', name = ~Combustible) %>%
layout(
xaxis = list(title = "Moth"),
yaxis = list(title = "Price per liter (€)"),
font = list(size = 13),
showlegend = TRUE,
legend = list(title = list(text = '<b> Fuel Type </b>'))) # Añade un título a la leyenda
})
}
shinyApp(ui, server)
app2 <- list(ui=ui,server = server)