### 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({ valueBox( paste(HAR_br_count(), tr("cases2")), tr("BR"), color = "purple" ) }) output$HAR_count_co <- renderValueBox({ valueBox( paste(HAR_co_count(), tr("cases2")), tr("CO"), color = "purple" ) }) # 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) } # Création du graphique 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) ) })