optimize code
Browse files- global.R +1 -0
- server.R +6 -122
- utils/render_time_series_plot.R +43 -0
global.R
CHANGED
|
@@ -7,6 +7,7 @@ library(leaflet.extras)
|
|
| 7 |
library(arrow) # For reading Parquet files
|
| 8 |
library(scales) # For color scaling
|
| 9 |
library(plotly)
|
|
|
|
| 10 |
|
| 11 |
# Read the metadata and availability data
|
| 12 |
tavg_meta <- read.csv("www/data/tabs/tavg_meta.csv")
|
|
|
|
| 7 |
library(arrow) # For reading Parquet files
|
| 8 |
library(scales) # For color scaling
|
| 9 |
library(plotly)
|
| 10 |
+
source("utils/render_time_series_plot.R", local = T)
|
| 11 |
|
| 12 |
# Read the metadata and availability data
|
| 13 |
tavg_meta <- read.csv("www/data/tabs/tavg_meta.csv")
|
server.R
CHANGED
|
@@ -145,48 +145,8 @@ shinyServer(function(input, output, session) {
|
|
| 145 |
station_id <- input$station_map_marker_click$id
|
| 146 |
month <- input$month
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
title = list(
|
| 151 |
-
text = paste(station_id, tavg_meta$NAME[tavg_meta$ID == station_id]),
|
| 152 |
-
y = 0.80,
|
| 153 |
-
font = list(size = 12) # Decrease title size here
|
| 154 |
-
),
|
| 155 |
-
xaxis = list(
|
| 156 |
-
zeroline = FALSE,
|
| 157 |
-
gridcolor = 'lightgray',
|
| 158 |
-
title = "", # Remove x-axis title here,
|
| 159 |
-
fixedrange = TRUE # Disable zoom on the y-axis
|
| 160 |
-
),
|
| 161 |
-
yaxis = list(
|
| 162 |
-
title = list(text = paste(month, '(°C)'), font = list(size = 10)),
|
| 163 |
-
zeroline = FALSE,
|
| 164 |
-
gridcolor = 'lightgray',
|
| 165 |
-
fixedrange = TRUE # Disable zoom on the y-axis
|
| 166 |
-
),
|
| 167 |
-
showlegend = FALSE,
|
| 168 |
-
plot_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 169 |
-
paper_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 170 |
-
margin = list(t = 60, b = 60, l = 60, r = 60), # Space for rounded corners
|
| 171 |
-
shapes = list(
|
| 172 |
-
list(
|
| 173 |
-
type = 'rect',
|
| 174 |
-
x0 = 0, x1 = 1, y0 = 0, y1 = 1,
|
| 175 |
-
xref = 'paper', yref = 'paper',
|
| 176 |
-
fillcolor = 'rgba(255, 255, 255, 0)', # Invisible border (roundness effect)
|
| 177 |
-
line = list(width = 0)
|
| 178 |
-
),
|
| 179 |
-
dragmode = FALSE, # Disable dragging (pan)
|
| 180 |
-
# Other layout options...
|
| 181 |
-
)
|
| 182 |
-
) %>%
|
| 183 |
-
add_trace(x = ~YEAR, y = fitted(lm(VALUE ~ YEAR, data = data)), mode = 'lines', name = 'Linear Trend') |>
|
| 184 |
-
config(
|
| 185 |
-
modeBarButtonsToRemove = list(
|
| 186 |
-
'zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'
|
| 187 |
-
),
|
| 188 |
-
displaylogo = FALSE # Optionally, remove the Plotly logo
|
| 189 |
-
)
|
| 190 |
})
|
| 191 |
|
| 192 |
|
|
@@ -202,46 +162,8 @@ shinyServer(function(input, output, session) {
|
|
| 202 |
|
| 203 |
month <- input$month
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
title = list(
|
| 208 |
-
text = paste(station_id, tavg_meta$NAME[tavg_meta$ID == station_id]),
|
| 209 |
-
y = 0.80,
|
| 210 |
-
font = list(size = 12) # Decrease title size here
|
| 211 |
-
),
|
| 212 |
-
xaxis = list(
|
| 213 |
-
zeroline = FALSE,
|
| 214 |
-
gridcolor = 'lightgray',
|
| 215 |
-
title = "",
|
| 216 |
-
fixedrange = TRUE # Remove x-axis title here
|
| 217 |
-
),
|
| 218 |
-
yaxis = list(
|
| 219 |
-
title = list(text = paste(month, '(°C)'), font = list(size = 10)),
|
| 220 |
-
zeroline = FALSE,
|
| 221 |
-
gridcolor = 'lightgray',
|
| 222 |
-
fixedrange = TRUE
|
| 223 |
-
),
|
| 224 |
-
showlegend = FALSE,
|
| 225 |
-
plot_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 226 |
-
paper_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 227 |
-
margin = list(t = 60, b = 60, l = 60, r = 60), # Space for rounded corners
|
| 228 |
-
shapes = list(
|
| 229 |
-
list(
|
| 230 |
-
type = 'rect',
|
| 231 |
-
x0 = 0, x1 = 1, y0 = 0, y1 = 1,
|
| 232 |
-
xref = 'paper', yref = 'paper',
|
| 233 |
-
fillcolor = 'rgba(255, 255, 255, 0)', # Invisible border (roundness effect)
|
| 234 |
-
line = list(width = 0)
|
| 235 |
-
)
|
| 236 |
-
)
|
| 237 |
-
) %>%
|
| 238 |
-
add_trace(x = ~YEAR, y = fitted(lm(VALUE ~ YEAR, data = data)), mode = 'lines', name = 'Linear Trend') |>
|
| 239 |
-
config(
|
| 240 |
-
modeBarButtonsToRemove = list(
|
| 241 |
-
'zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'
|
| 242 |
-
),
|
| 243 |
-
displaylogo = FALSE # Optionally, remove the Plotly logo
|
| 244 |
-
)
|
| 245 |
})
|
| 246 |
})
|
| 247 |
|
|
@@ -257,46 +179,8 @@ shinyServer(function(input, output, session) {
|
|
| 257 |
data <- time_series_data() # Get the filtered time series data
|
| 258 |
req(nrow(data) > 0) # Ensure there is data to plot
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
title = list(
|
| 263 |
-
text = paste(station_id, tavg_meta$NAME[tavg_meta$ID == station_id]),
|
| 264 |
-
y = 0.80,
|
| 265 |
-
font = list(size = 12) # Decrease title size here
|
| 266 |
-
),
|
| 267 |
-
xaxis = list(
|
| 268 |
-
zeroline = FALSE,
|
| 269 |
-
gridcolor = 'lightgray',
|
| 270 |
-
title = "",
|
| 271 |
-
fixedrange = TRUE # Remove x-axis title here
|
| 272 |
-
),
|
| 273 |
-
yaxis = list(
|
| 274 |
-
title = list(text = paste(month, '(°C)'), font = list(size = 10)),
|
| 275 |
-
zeroline = FALSE,
|
| 276 |
-
gridcolor = 'lightgray',
|
| 277 |
-
fixedrange = TRUE
|
| 278 |
-
),
|
| 279 |
-
showlegend = FALSE,
|
| 280 |
-
plot_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 281 |
-
paper_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 282 |
-
margin = list(t = 60, b = 60, l = 60, r = 60), # Space for rounded corners
|
| 283 |
-
shapes = list(
|
| 284 |
-
list(
|
| 285 |
-
type = 'rect',
|
| 286 |
-
x0 = 0, x1 = 1, y0 = 0, y1 = 1,
|
| 287 |
-
xref = 'paper', yref = 'paper',
|
| 288 |
-
fillcolor = 'rgba(255, 255, 255, 0)', # Invisible border (roundness effect)
|
| 289 |
-
line = list(width = 0)
|
| 290 |
-
)
|
| 291 |
-
)
|
| 292 |
-
) %>%
|
| 293 |
-
add_trace(x = ~YEAR, y = fitted(lm(VALUE ~ YEAR, data = data)), mode = 'lines', name = 'Linear Trend') |>
|
| 294 |
-
config(
|
| 295 |
-
modeBarButtonsToRemove = list(
|
| 296 |
-
'zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'
|
| 297 |
-
),
|
| 298 |
-
displaylogo = FALSE # Optionally, remove the Plotly logo
|
| 299 |
-
)
|
| 300 |
})
|
| 301 |
})
|
| 302 |
|
|
|
|
| 145 |
station_id <- input$station_map_marker_click$id
|
| 146 |
month <- input$month
|
| 147 |
|
| 148 |
+
render_time_series_plot(data, station_id, month)
|
| 149 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
})
|
| 151 |
|
| 152 |
|
|
|
|
| 162 |
|
| 163 |
month <- input$month
|
| 164 |
|
| 165 |
+
render_time_series_plot(data, station_id, month)
|
| 166 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
})
|
| 168 |
})
|
| 169 |
|
|
|
|
| 179 |
data <- time_series_data() # Get the filtered time series data
|
| 180 |
req(nrow(data) > 0) # Ensure there is data to plot
|
| 181 |
|
| 182 |
+
render_time_series_plot(data, station_id, month)
|
| 183 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
})
|
| 185 |
})
|
| 186 |
|
utils/render_time_series_plot.R
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Helper function to render the time series plot
|
| 2 |
+
render_time_series_plot <- function(data, station_id, month) {
|
| 3 |
+
plot_ly(data, x = ~YEAR, y = ~VALUE, type = 'scatter', mode = 'lines+markers', name = 'TAVG') %>%
|
| 4 |
+
layout(
|
| 5 |
+
title = list(
|
| 6 |
+
text = paste(station_id, tavg_meta$NAME[tavg_meta$ID == station_id]),
|
| 7 |
+
y = 0.80,
|
| 8 |
+
font = list(size = 12) # Decrease title size here
|
| 9 |
+
),
|
| 10 |
+
xaxis = list(
|
| 11 |
+
zeroline = FALSE,
|
| 12 |
+
gridcolor = 'lightgray',
|
| 13 |
+
title = "", # Remove x-axis title here
|
| 14 |
+
fixedrange = TRUE # Disable zoom on the y-axis
|
| 15 |
+
),
|
| 16 |
+
yaxis = list(
|
| 17 |
+
title = list(text = paste(month, '(°C)'), font = list(size = 10)),
|
| 18 |
+
zeroline = FALSE,
|
| 19 |
+
gridcolor = 'lightgray',
|
| 20 |
+
fixedrange = TRUE # Disable zoom on the y-axis
|
| 21 |
+
),
|
| 22 |
+
showlegend = FALSE,
|
| 23 |
+
plot_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 24 |
+
paper_bgcolor = 'rgba(255, 255, 255, 0)', # Semi-transparent background
|
| 25 |
+
margin = list(t = 60, b = 60, l = 60, r = 60), # Space for rounded corners
|
| 26 |
+
shapes = list(
|
| 27 |
+
list(
|
| 28 |
+
type = 'rect',
|
| 29 |
+
x0 = 0, x1 = 1, y0 = 0, y1 = 1,
|
| 30 |
+
xref = 'paper', yref = 'paper',
|
| 31 |
+
fillcolor = 'rgba(255, 255, 255, 0)', # Invisible border (roundness effect)
|
| 32 |
+
line = list(width = 0)
|
| 33 |
+
)
|
| 34 |
+
)
|
| 35 |
+
) %>%
|
| 36 |
+
add_trace(x = ~YEAR, y = fitted(lm(VALUE ~ YEAR, data = data)), mode = 'lines', name = 'Linear Trend') %>%
|
| 37 |
+
config(
|
| 38 |
+
modeBarButtonsToRemove = list(
|
| 39 |
+
'zoom2d', 'pan2d', 'select2d', 'lasso2d', 'zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d'
|
| 40 |
+
),
|
| 41 |
+
displaylogo = FALSE # Optionally, remove the Plotly logo
|
| 42 |
+
)
|
| 43 |
+
}
|