Skip to content

Commit c0b499f

Browse files
committed
add kpi in spain and kpi relative maps in spain
1 parent 63549bb commit c0b499f

File tree

7 files changed

+181
-88
lines changed

7 files changed

+181
-88
lines changed

data/pob_ccaa.rds

577 Bytes
Binary file not shown.

load_data.R

+46-78
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ library(shinydashboard)
77
library(htmltools)
88
library(leafpop)
99

10-
1110
source(paste0(getwd(), "/model/generate_data.R"))
1211
# source(paste0(getwd(), "/model/generate_ccaa_data.R"))
1312

@@ -106,8 +105,12 @@ cvirus_map_data <- res %>%
106105
fallecidos = sum(fallecidos)) %>%
107106
ungroup()
108107

109-
var_global_list <- c("casos","recuperados", "fallecidos", "casos_nuevos",
110-
"recuperados_nuevos", "fallecidos_nuevos")
108+
var_ccaa_list <- c("casos_por_100_mil_habitantes",
109+
"recuperados_por_100_mil_habitantes",
110+
"fallecidos_por_100_mil_habitantes",
111+
"casos_nuevos_por_100_mil_habitantes",
112+
"recuperados_diarios_por_100_mil_habitantes",
113+
"fallecidos_diarios_por_100_mil_habitantes")
111114

112115
## TODO try to change to highcharter and conver to interactive shiny
113116

@@ -291,6 +294,10 @@ ccaa_longer <- ccaa_casos_longer %>%
291294
by = c("cod_ine", "fecha"))
292295
ccaa_longer$fecha <- as.Date(as.character(ccaa_longer$fecha), format = "%d/%m/%y")
293296

297+
pob_ccaa <- readRDS("data/pob_ccaa.rds")
298+
299+
ccaa_longer <- ccaa_longer %>% left_join(pob_ccaa, by = c("cod_ine" = "Codigo"))
300+
294301
## Mapa ccaa ----
295302

296303
mapa_ccaa <- readRDS("data/mapa_ccaa.rds")
@@ -303,7 +310,8 @@ ccaa_data_subplots <- ccaa_longer %>%
303310
casos = sum(casos),
304311
recuperados = sum(altas),
305312
fallecidos = sum(fallecidos),
306-
ingresos_uci = sum(ingresos_uci)
313+
ingresos_uci = sum(ingresos_uci),
314+
pob2019 = first(pob2019)
307315
) %>%
308316
mutate(
309317
casos_prev_day = lag(casos, n = 1, default = 0),
@@ -325,14 +333,16 @@ ccaa_data_subplots <- ccaa_longer %>%
325333

326334

327335
ccaa_map_data <- ccaa_data_subplots %>%
328-
filter(CCAA != "Total") %>%
329-
group_by(cod_ine, CCAA) %>%
330-
filter(fecha == max(fecha)) %>%
331-
mutate(casos = sum(casos),
332-
recuperados = sum(recuperados),
333-
fallecidos = sum(fallecidos),
334-
ingresos_uci = sum(ingresos_uci, na.rm =TRUE)
335-
) %>%
336+
filter(CCAA != "Total" & fecha == max(fecha)) %>%
337+
mutate (
338+
casos_por_100_mil_habitantes = 1e5 * casos / pob2019,
339+
recuperados_por_100_mil_habitantes = 1e5 * recuperados / pob2019,
340+
fallecidos_por_100_mil_habitantes = 1e5 * fallecidos / pob2019,
341+
casos_nuevos_por_100_mil_habitantes = 1e5 * casos_nuevos / pob2019,
342+
recuperados_diarios_por_100_mil_habitantes = 1e5 * recuperados_nuevos / pob2019,
343+
fallecidos_diarios_por_100_mil_habitantes = 1e5 * fallecidos_nuevos / pob2019
344+
345+
) %>%
336346
ungroup()
337347

338348

@@ -379,70 +389,28 @@ mapa_ccaa <- mapa_ccaa %>%
379389
inner_join(ccaa_map_data, by = c("Codigo" = "cod_ine") )
380390

381391

382-
mapa_ccaa_leaflet <-
383-
leaflet(mapa_ccaa) %>%
384-
addProviderTiles("Stamen.Toner") %>%
385-
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
386-
group = "Ingresos UCI",
387-
opacity = 1.0, fillOpacity = 0.5,
388-
fillColor = ~colorNumeric("Reds", ingresos_uci)(ingresos_uci),
389-
label = lapply(mapa_ccaa$labs, htmltools::HTML),
390-
highlightOptions = highlightOptions(color = "white", weight = 2,
391-
bringToFront = TRUE)) %>%
392-
addLegend("bottomleft", pal = colorNumeric("Reds", mapa_ccaa$ingresos_uci),
393-
values = ~ingresos_uci,
394-
title = "Ingresos UCI",
395-
group = "Ingresos UCI",
396-
# labFormat = labelFormat(prefix = "$"),
397-
opacity = 1) %>%
398-
399-
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
400-
group = "Casos",
401-
opacity = 1.0, fillOpacity = 0.5,
402-
fillColor = ~colorNumeric("Blues", casos)(casos),
403-
label = lapply(mapa_ccaa$labs, htmltools::HTML),
404-
highlightOptions = highlightOptions(color = "white", weight = 2,
405-
bringToFront = TRUE)) %>%
406-
addLegend("bottomleft", pal = colorNumeric("Blues", mapa_ccaa$casos),
407-
values = ~casos,
408-
title = "Casos",
409-
group = "Casos",
410-
# labFormat = labelFormat(prefix = "$"),
411-
opacity = 1) %>%
412-
413-
414-
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
415-
group = "Fallecidos",
416-
opacity = 1.0, fillOpacity = 0.5,
417-
fillColor = ~colorNumeric("Reds", fallecidos)(fallecidos),
418-
label = lapply(mapa_ccaa$labs, htmltools::HTML),
419-
highlightOptions = highlightOptions(color = "white", weight = 2,
420-
bringToFront = TRUE)) %>%
421-
addLegend("bottomright", pal = colorNumeric("Reds", mapa_ccaa$fallecidos),
422-
values = ~fallecidos,
423-
title = "Fallecidos",
424-
group = "Fallecidos",
425-
# labFormat = labelFormat(prefix = "$"),
426-
opacity = 1) %>%
427-
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
428-
group = "Recuperados",
429-
opacity = 1.0, fillOpacity = 0.5,
430-
fillColor = ~colorNumeric("Greens", recuperados)(recuperados),
431-
label = lapply(mapa_ccaa$labs, htmltools::HTML),
432-
highlightOptions = highlightOptions(color = "white", weight = 2,
433-
bringToFront = TRUE)) %>%
434-
addLegend("bottomright", pal = colorNumeric("Greens", mapa_ccaa$recuperados),
435-
values = ~recuperados,
436-
title = "Recuperados",
437-
group = "Recuperados",
438-
# labFormat = labelFormat(prefix = "$"),
439-
opacity = 1) %>%
440-
441-
# Layers control
442-
addLayersControl(
443-
baseGroups = c("Casos", "Ingresos UCI","Fallecidos","Recuperados" ),
444-
# overlayGroups = c("Quakes", "Outline"),
445-
options = layersControlOptions(collapsed = FALSE)
446-
)
392+
pal1 <- colorNumeric(
393+
palette = "Reds",
394+
domain = mapa_ccaa$casos_por_100_mil_habitantes)
447395

448-
396+
mapa_ccaa_leaflet <-
397+
leaflet(mapa_ccaa) %>%
398+
addProviderTiles("Stamen.Toner") %>%
399+
addPolygons(
400+
color = "#444444",
401+
weight = 1,
402+
smoothFactor = 0.5,
403+
# group = "Ingresos UCI",
404+
opacity = 1.0,
405+
fillOpacity = 0.5,
406+
fillColor = ~ pal1(casos_por_100_mil_habitantes),
407+
label = lapply(mapa_ccaa$labs, htmltools::HTML),
408+
highlightOptions = highlightOptions(
409+
color = "white",
410+
weight = 2,
411+
bringToFront = TRUE
412+
)) %>%
413+
addLegend(position = "topleft",
414+
pal = pal1,
415+
values = ~casos_por_100_mil_habitantes,
416+
title = "casos_por_100_mil_habitantes")

server_dir/map_ccaa_server.R

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
11
library(shinydashboard)
22

33

4-
output$mapa_ccaa <- renderLeaflet({
4+
5+
# paleta reactiva
6+
colorpal <- reactive({
7+
colorNumeric("Reds", mapa_ccaa[[input$var_ccaa]])
8+
})
9+
10+
# mapa base con
11+
output$mapa_ccaa_base <- renderLeaflet({
512
mapa_ccaa_leaflet
613
})
714

15+
16+
# Mapa reactiva
17+
observe({
18+
pal <- colorpal()
19+
20+
leafletProxy("mapa_ccaa_base", data = mapa_ccaa) %>%
21+
# clearShapes() %>%
22+
addPolygons(
23+
color = "#444444",
24+
weight = 1,
25+
smoothFactor = 0.5,
26+
# group = "Ingresos UCI",
27+
opacity = 1.0,
28+
fillOpacity = 0.5,
29+
fillColor = ~ pal( mapa_ccaa[[input$var_ccaa]]),
30+
label = lapply(mapa_ccaa$labs, htmltools::HTML),
31+
highlightOptions = highlightOptions(
32+
color = "white",
33+
weight = 2,
34+
bringToFront = TRUE
35+
)
36+
)
37+
38+
})
39+
40+
# Leynda reactivos
41+
observe({
42+
proxy <- leafletProxy("mapa_ccaa_base", data = mapa_ccaa)
43+
44+
# Remove any existing legend, and only if the legend is
45+
# enabled, create a new one.
46+
proxy %>% clearControls()
47+
pal <- colorpal()
48+
proxy %>% addLegend(position = "topleft",
49+
pal = pal, values = ~mapa_ccaa[[input$var_ccaa]],
50+
title = input$var_ccaa
51+
)
52+
})
53+
54+
# Fecha máxima
855
output$max_fecha_ccaa <- renderText({
956
fecha_max <- ccaa_longer %>%
1057
summarise(fecha = max(fecha)) %>%

server_dir/simple_kpis_server.R

+60-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
2+
## global ----
33
output$contagiados <- renderValueBox({
44
casos_total <- cvirus_map_data %>%
55
filter(fecha == max(fecha)) %>%
@@ -12,7 +12,7 @@ output$contagiados <- renderValueBox({
1212

1313
output$recuperados <- renderValueBox({
1414
recuperados_total <- cvirus_map_data %>%
15-
filter(fecha == max(fecha)) %>%
15+
# filter(fecha == max(fecha)) %>%
1616
summarise(recuperados_total = sum(recuperados)) %>%
1717
pull(recuperados_total)
1818

@@ -21,9 +21,66 @@ output$recuperados <- renderValueBox({
2121

2222
output$fallecidos <- renderValueBox({
2323
fallecidos_total <- cvirus_map_data %>%
24+
# filter(fecha == max(fecha)) %>%
25+
summarise(fallecidos_total = sum(fallecidos)) %>%
26+
pull(fallecidos_total)
27+
28+
valueBox(fallecidos_total, subtitle = "Fallecidos", color = "red")
29+
})
30+
31+
output$activos <- renderValueBox({
32+
activos_total <- cvirus_map_data %>%
33+
# filter(fecha == max(fecha)) %>%
34+
summarise(casos_total = sum(casos),
35+
fallecidos_total = sum(fallecidos),
36+
recuperados_total = sum(recuperados)) %>%
37+
mutate(activos_total = casos_total - fallecidos_total - recuperados_total) %>%
38+
pull(activos_total)
39+
40+
valueBox(activos_total, subtitle = "Casos activos ", color = "orange")
41+
})
42+
43+
44+
## Spain -----
45+
46+
output$sp_contagiados <- renderValueBox({
47+
casos_total <- mapa_ccaa %>%
2448
filter(fecha == max(fecha)) %>%
49+
summarise(casos_total = sum(casos)) %>%
50+
pull(casos_total)
51+
52+
valueBox(casos_total, subtitle = "Contagiados", color = "black")
53+
})
54+
55+
56+
output$sp_recuperados <- renderValueBox({
57+
recuperados_total <- mapa_ccaa %>%
58+
# filter(fecha == max(fecha)) %>%
59+
summarise(recuperados_total = sum(recuperados)) %>%
60+
pull(recuperados_total)
61+
62+
valueBox(recuperados_total, subtitle = "Recuperados", color = "green")
63+
})
64+
65+
output$sp_fallecidos <- renderValueBox({
66+
fallecidos_total <- mapa_ccaa %>%
67+
# filter(fecha == max(fecha)) %>%
2568
summarise(fallecidos_total = sum(fallecidos)) %>%
2669
pull(fallecidos_total)
2770

2871
valueBox(fallecidos_total, subtitle = "Fallecidos", color = "red")
29-
})
72+
})
73+
74+
output$sp_activos <- renderValueBox({
75+
activos_total <- mapa_ccaa %>%
76+
# filter(fecha == max(fecha)) %>%
77+
summarise(casos_total = sum(casos),
78+
fallecidos_total = sum(fallecidos),
79+
recuperados_total = sum(recuperados)) %>%
80+
mutate(activos_total = casos_total - fallecidos_total - recuperados_total) %>%
81+
pull(activos_total)
82+
83+
valueBox(activos_total, subtitle = "Casos activos ", color = "orange")
84+
})
85+
86+

ui.R

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ header <- dashboardHeader( title = "Corona virus \n dashboard",
1313
titleWidth = ancho_titulo_side
1414
)
1515

16-
1716
# sidebar
1817
sidebar <- dashboardSidebar(
1918
width = ancho_titulo_side,

ui_dir/map_ccaa_ui.R

+24-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,31 @@ library(shinydashboard)
33

44
tab_mapa_ccaa <- tabItem(
55
tabName = "Mapa_ccaa",
6-
# h1("Mapa a día"),
7-
h2(textOutput("max_fecha_ccaa")),
6+
h2("Fecha actualización datos: ") , h2(textOutput("max_fecha_ccaa")),
7+
fluidRow(
8+
valueBoxOutput("sp_contagiados"),
9+
valueBoxOutput("sp_recuperados"),
10+
valueBoxOutput("sp_fallecidos"),
11+
valueBoxOutput("sp_activos")
12+
),
813
br(),
914
fluidRow(
10-
leafletOutput("mapa_ccaa", width="100%",height="600px")
15+
16+
17+
column(3,
18+
wellPanel(
19+
selectInput('var_ccaa', 'Elige variables', choices = var_ccaa_list,
20+
selected = var_ccaa_list[1], width = 280)
21+
22+
)
23+
),
24+
25+
column(9,
26+
leafletOutput("mapa_ccaa_base", width="100%",height="600px")
27+
28+
)
1129
)
30+
# fluidRow(
31+
# leafletOutput("mapa_ccaa", width="100%",height="600px")
32+
# )
1233
)

ui_dir/map_ui.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ library(shinydashboard)
99
fluidRow(
1010
valueBoxOutput("contagiados"),
1111
valueBoxOutput("recuperados"),
12-
valueBoxOutput("fallecidos")
12+
valueBoxOutput("fallecidos"),
13+
valueBoxOutput("activos")
1314
),
1415
br(),
15-
h5("Radio círculos = 3 * log( fallecidos + 1 )"),
16+
# h5("Radio círculos = 3 * log( fallecidos + 1 )"),
1617
fluidRow(
1718
leafletOutput("mapa_global", width="100%",height="600px")
1819
)

0 commit comments

Comments
 (0)