Newer
Older
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
### Fichier harmonized.R - Module d'indicateurs harmonisés pour l'analyse du paludisme
### à la frontière Brésil-Colombie
# Fonctions réactives pour la gestion des dates
date1 <- reactive({
as.Date(paste0(input$dates[1],"-01-01"))
})
date2 <- reactive({
as.Date(paste0(input$dates[2],"-12-31"))
})
# Filtrage principal des données
HAR_sub <- reactive({
date1 <- as.Date(paste0(input$dates[1],"-01-01"))
date2 <- as.Date(paste0(input$dates[2],"-12-31"))
consultation_har_filter(JSON_cons_har,
new_attack = input$new_attack,
diagn = input$diagn,
sex = input$sex,
minAge = input$age[1],
maxAge = input$age[2]
) %>%
filter(consultation_date >= date1 & consultation_date <= date2)
})
# Séparation des données par pays
HAR_br <- reactive({
HAR_sub() %>% filter(source == "BR")
})
HAR_co <- reactive({
# Vérification au niveau du filtrage initial
initial_data <- HAR_sub() %>% filter(source == "CO")
print("Données colombiennes après filtrage initial :")
print(paste("Nombre de cas :", nrow(initial_data)))
print("Exemple de dates :")
print(head(initial_data$consultation_date))
return(initial_data)
})
# Création des tableaux temporels pour le Brésil
HAR_br_tab <- reactive({
agg <- as.numeric(input$agg)
date1 <- as.Date(paste0(input$dates[1],"-01-01"))
date2 <- as.Date(paste0(input$dates[2],"-12-31"))
if(agg == 1){
# Agrégation journalière
HAR_br() %>%
group_by(dates = floor_date(consultation_date, "day")) %>%
summarize(counts=n()) %>%
complete(dates = seq.Date(date1, date2, by = "day"),
fill = list(counts = 0))
} else if(agg == 6){
# Agrégation hebdomadaire
as.data.frame(incidence(HAR_br()$consultation_date,
interval = 7,
iso_week = TRUE)) %>%
select(dates, counts)
} else if(agg == 30){
# Agrégation mensuelle
HAR_br() %>%
group_by(dates = floor_date(consultation_date, "month")) %>%
summarize(counts=n()) %>%
complete(dates = seq.Date(date1, date2, by = "month"),
fill = list(counts = 0))
} else if(agg == 90){
# Agrégation trimestrielle
HAR_br() %>%
group_by(dates = floor_date(consultation_date, "quarter")) %>%
summarize(counts=n()) %>%
complete(dates = seq.Date(date1, date2, by = "quarter"),
fill = list(counts = 0))
} else if(agg == 366){
# Agrégation annuelle
HAR_br() %>%
group_by(dates = floor_date(consultation_date, "year")) %>%
summarize(counts=n()) %>%
complete(dates = seq.Date(date1, date2, by = "year"),
fill = list(counts = 0))
}
})
# Création des tableaux temporels pour la Colombie
HAR_co_tab <- reactive({
agg <- as.numeric(input$agg)
date1 <- as.Date(paste0(input$dates[1],"-01-01"))
date2 <- as.Date(paste0(input$dates[2],"-12-31"))
# Création d'une série temporelle complète avec des zéros pour les périodes sans cas
data_co <- HAR_co() %>%
group_by(dates = floor_date(consultation_date, "month")) %>%
summarize(counts = n()) %>%
# Cette ligne est cruciale : elle crée tous les mois, même ceux sans cas
complete(dates = seq.Date(date1, date2, by = "month"),
fill = list(counts = 0))
return(data_co)
})
##################
### Visualisations et compteurs
##################
# Fonction réactive pour compter les cas colombiens
HAR_co_count <- reactive({
nrow(HAR_co())
})
# Fonction réactive pour compter les cas brésiliens
HAR_br_count <- reactive({
nrow(HAR_br())
})
# Rendu des compteurs
output$HAR_count_br <- renderValueBox({
paste(HAR_br_count(), tr("cases2")),
tr("BR"),
color = "purple"
paste(HAR_co_count(), tr("cases2")),
tr("CO"),
color = "purple"
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
)
})
# Graphique d'incidence
output$HAR_plot_incidence <- renderPlotly({
plot_ly() %>%
add_trace(
data = HAR_br_tab(),
x = ~dates,
y = ~counts,
type = 'scatter',
mode = 'lines',
line = list(color = 'blue', width = 2),
name = paste0("Brazil (", text_recup_donnees_BR, ")")
) %>%
add_trace(
data = HAR_co_tab(),
x = ~dates,
y = ~counts,
type = 'scatter',
mode = 'lines',
line = list(color = 'red', width = 2),
name = paste0("Colombia (", text_recup_donnees_CO, ")")
) %>%
layout(
xaxis = list(title = "Date"),
yaxis = list(
title = "Cases",
type = 'linear', # Échelle linéaire au lieu de logarithmique
tickformat = 'd' # Format entier pour les nombres
),
showlegend = TRUE,
legend = list(x = 0, y = 1.2)
)
})
# Graphique des âges
output$HAR_plot_age <- renderPlotly({
# Préparation des données
HAR_br <- HAR_br() %>% mutate(patient_age = round(patient_age, 0))
HAR_co <- HAR_co() %>% mutate(patient_age = round(patient_age, 0))
# Création du graphique
plot_ly() %>%
add_trace(data = HAR_br,
x = ~patient_age,
type = "histogram",
name = tr("BR")) %>%
add_trace(data = HAR_co,
x = ~patient_age,
type = "histogram",
name = tr("CO")) %>%
layout(xaxis = list(title = tr("age"), domain = c(0,100)),
yaxis = list(title = tr("cases"))) %>%
layout(legend = list(x = 0.8, y = 0.9))
})
# Graphique de la répartition par sexe
output$HAR_plot_sex <- renderPlotly({
# Liste des pays
HAR_loc <- c("CO","BR")
# Filtrage des données par sexe
HAR_br_sex_sub <- subset(HAR_br(), patient_sex == "Male" | patient_sex == "Female")
HAR_co_sex_sub <- subset(HAR_co(), patient_sex == "Male" | patient_sex == "Female")
# Calcul des proportions
HAR_br_sex_tab <- prop.table(table(HAR_br_sex_sub$patient_sex))*100
HAR_co_sex_tab <- prop.table(table(HAR_co_sex_sub$patient_sex))*100
HAR_male <- c(HAR_co_sex_tab["Male"],HAR_br_sex_tab["Male"])
HAR_female <- c(HAR_co_sex_tab["Female"],HAR_br_sex_tab["Female"])
HAR_data <- data.frame(HAR_loc, HAR_male, HAR_female)
# Création du graphique
plot_ly(HAR_data,
x = ~HAR_loc,
y = ~HAR_male,
type = "bar",
name = tr("male")) %>%
add_trace(y = ~HAR_female, name = tr("female")) %>%
layout(xaxis = list(title = ""),
yaxis = list(title = tr("percentage")),
barmode = 'stack')
})
# Graphique de la répartition par type de Plasmodium
output$HAR_plot_plasm <- renderPlotly({
# Préparation des données
results <- tibble(
country = character(),
type = character(),
percentage = numeric()
)
# Traitement des données brésiliennes
br_total <- nrow(HAR_br())
if (br_total > 0) {
br_results <- tibble(
country = "Brazil",
type = c("Falciparum", "Mixed", "Non-falciparum"),
percentage = c(
sum(HAR_br()$diagnosis_result == "falciparum") / br_total * 100,
sum(HAR_br()$diagnosis_result %in% c("mixed infection with P. falciparum",
"mixte infection with P. falciparum")) / br_total * 100,
sum(HAR_br()$diagnosis_result == "non-falciparum") / br_total * 100
)
)
results <- bind_rows(results, br_results)
}
# Traitement des données colombiennes
co_total <- nrow(HAR_co())
if (co_total > 0) {
co_results <- tibble(
country = "Colombia",
type = c("Falciparum", "Mixed", "Non-falciparum"),
percentage = c(
sum(HAR_co()$diagnosis_result == "falciparum") / co_total * 100,
sum(HAR_co()$diagnosis_result %in% c("mixed infection with P. falciparum",
"mixte infection with P. falciparum")) / co_total * 100,
sum(HAR_co()$diagnosis_result == "non-falciparum") / co_total * 100
)
)
results <- bind_rows(results, co_results)
}
plot_ly(data = results,
x = ~country,
y = ~percentage,
type = "bar",
color = ~type,
colors = c("#1f77b4", "#ff7f0e", "#2ca02c")) %>%
layout(
barmode = "stack",
yaxis = list(title = "Percentage of cases"),
showlegend = TRUE,
legend = list(x = 0.7, y = 0.9)
)