-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.qmd
More file actions
134 lines (117 loc) · 4.57 KB
/
Copy pathhistory.qmd
File metadata and controls
134 lines (117 loc) · 4.57 KB
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
---
title: "Evolución"
format:
html:
toc: true
page-layout: full
execute:
echo: false
warning: false
message: false
---
```{r}
source("R/page_helpers.R")
vf_load_packages()
history <- vf_read_csv(c("data/processed/dashboard_history.csv", "assets/history/dashboard_history.csv"))
```
## Histórico del visor
```{r}
if (nrow(history) == 0) {
knitr::asis_output("_No hay histórico disponible todavía. Ejecuta `scripts/09_update_dashboard_history.R` antes de renderizar si quieres alimentar esta página._\n")
} else {
date_col <- vf_history_date_column(history)
if (!is.na(date_col)) {
history[[date_col]] <- vf_as_date(history[[date_col]])
history <- history |> dplyr::arrange(.data[[date_col]])
}
summary <- tibble::tibble(
indicador = c("Registros", "Primera fecha", "Última fecha", "Archivo"),
valor = c(
as.character(nrow(history)),
if (!is.na(date_col)) as.character(vf_safe_min_date(history[[date_col]])) else "sin columna de fecha",
if (!is.na(date_col)) as.character(vf_safe_max_date(history[[date_col]])) else "sin columna de fecha",
attr(history, "source_path") %||% "-"
)
)
vf_kable(summary)
}
```
## Últimos registros
```{r}
if (nrow(history) > 0) {
date_col <- vf_history_date_column(history)
if (!is.na(date_col)) {
history[[date_col]] <- vf_as_date(history[[date_col]])
show_history <- history |> dplyr::arrange(dplyr::desc(.data[[date_col]])) |> utils::head(20)
} else {
show_history <- history |> utils::head(20)
}
vf_kable(show_history, caption = "Últimos registros del histórico")
}
```
## Métricas principales
```{r}
if (nrow(history) > 0) {
metrics <- vf_numeric_metric_columns(history)
# Priorizamos nombres habituales; si no existen, se muestran las primeras métricas numéricas.
priority <- c(
"aemet_layers", "aemet_n_layers", "n_aemet_layers",
"firms_total", "n_firms", "firms_last_24h", "firms_24h",
"alerts_total", "n_alerts",
"effis_layers", "n_effis_layers"
)
selected <- unique(c(intersect(priority, metrics), metrics))
selected <- selected[seq_len(min(length(selected), 8))]
if (length(selected) > 0) {
last_row <- history[nrow(history), , drop = FALSE]
metric_table <- tibble::tibble(
metrica = selected,
ultimo_valor = as.numeric(last_row[1, selected, drop = TRUE])
)
if (nrow(history) >= 2) {
previous_row <- history[nrow(history) - 1L, , drop = FALSE]
metric_table$valor_anterior <- as.numeric(previous_row[1, selected, drop = TRUE])
metric_table$cambio <- metric_table$ultimo_valor - metric_table$valor_anterior
}
vf_kable(metric_table, caption = "Último valor de métricas numéricas")
} else {
knitr::asis_output("_El histórico no contiene métricas numéricas._\n")
}
}
```
## Gráficos de evolución
```{r}
if (nrow(history) > 1 && requireNamespace("ggplot2", quietly = TRUE) && requireNamespace("tidyr", quietly = TRUE)) {
date_col <- vf_history_date_column(history)
metrics <- vf_numeric_metric_columns(history)
priority <- c(
"aemet_layers", "aemet_n_layers", "n_aemet_layers",
"firms_total", "n_firms", "firms_last_24h", "firms_24h",
"alerts_total", "n_alerts",
"effis_layers", "n_effis_layers"
)
selected <- unique(c(intersect(priority, metrics), metrics))
selected <- selected[seq_len(min(length(selected), 6))]
if (!is.na(date_col) && length(selected) > 0) {
plot_data <- history |>
dplyr::select(fecha = dplyr::all_of(date_col), dplyr::all_of(selected)) |>
tidyr::pivot_longer(cols = -fecha, names_to = "metrica", values_to = "valor") |>
dplyr::filter(!is.na(.data$fecha), !is.na(.data$valor))
if (nrow(plot_data) > 0) {
ggplot2::ggplot(plot_data, ggplot2::aes(x = .data$fecha, y = .data$valor)) +
ggplot2::geom_line() +
ggplot2::geom_point() +
ggplot2::facet_wrap(~ metrica, scales = "free_y") +
ggplot2::labs(x = NULL, y = NULL)
} else {
knitr::asis_output("_No hay datos suficientes para el gráfico._\n")
}
} else {
knitr::asis_output("_No se encuentra una columna de fecha o métricas adecuadas para graficar._\n")
}
} else if (nrow(history) > 1) {
knitr::asis_output("_Para mostrar gráficos instala `ggplot2` y `tidyr`._\n")
}
```
## Nota sobre AEMET y EFFIS
La evolución debe interpretarse con la misma lógica que el mapa principal: AEMET publica la última emisión disponible, y las fechas antiguas no deben reaparecer si la limpieza previa se ha ejecutado correctamente. EFFIS puede aparecer como cero o ausente mientras no haya una capa WMS actual con contenido visual publicable.