Skip to content
Snippets Groups Projects
infoq.R 11 KiB
Newer Older
Vincent DUPONT's avatar
Vincent DUPONT committed
### Information quality tab

##################
### Data objects
##################

INFOQ_plot_tab <- reactive({
  
  date1 <- as.Date(paste0(input$INFOQdates[1],"-01-01"))
  date2 <- as.Date(paste0(input$INFOQdates[2],"-12-31"))
  
  consultation_har_filter(JSON_cons_har,
                          source = input$INFOQcountry,
                          new_attack = input$INFOQnew_attack,
                          diagn = input$INFOQdiagn,
                          sex = input$INFOQsex
  ) %>%
    filter(consultation_date >= date1 & consultation_date <= date2) %>%
    mutate(year = lubridate::year(consultation_date)) %>%
    select(residence_place,
           infection_place,
           id_center, 
           patient_age,
           patient_sex,
           diagnosis_result,
           new_attack,
           active_diagnosis,
           source,
           year
    ) %>%
    group_by(source, year) %>%
    dplyr::summarise(
      count = n(), 
      residence_place = mean(is.na(residence_place))*100,
      infection_place = mean(is.na(infection_place))*100,
      center = mean(is.na(id_center))*100,
      patient_age = mean(is.na(patient_age))*100,
      sex = mean(is.na(patient_sex))*100,
      diagnosis_result = mean(is.na(diagnosis_result))*100,
      new_attack = mean(is.na(new_attack))*100,
      active_diagnosis = mean(is.na(active_diagnosis))*100
    )
})

INFOQ_plot_tab_fg <- reactive({
  INFOQ_plot_tab() %>% filter(source == "FR-GF")
})
INFOQ_plot_tab_br <- reactive({
  INFOQ_plot_tab() %>% filter(source == "BR")
})

INFOQ_tab <- reactive({
  
  date1 <- as.Date(paste0(input$INFOQdates[1],"-01-01"))
  date2 <- as.Date(paste0(input$INFOQdates[2],"-12-31"))
  
  if(input$INFOQtype == "residence_area"){
    consultation_har_filter(JSON_cons_har,
                            source = input$INFOQcountry,
                            new_attack = input$INFOQnew_attack,
                            diagn = input$INFOQdiagn,
                            sex = input$INFOQsex
    ) %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      select(residence_place,
             infection_place,
             id_center, 
             patient_age,
             patient_sex,
             diagnosis_result,
             new_attack,
             active_diagnosis
      ) %>%
      group_by(residence_place) %>%
      dplyr::summarise(
        count = n(), 
        infection_place = percent(mean(is.na(infection_place)), format = "d"),
        center = percent(mean(is.na(id_center)), format = "d"),
        patient_age = percent(mean(is.na(patient_age)), format = "d"),
        sex = percent(mean(is.na(patient_sex)), format = "d"),
        diagnosis_result = percent(mean(is.na(diagnosis_result)), format = "d"),
        new_attack = percent(mean(is.na(new_attack)), format = "d"),
        active_diagnosis = percent(mean(is.na(active_diagnosis)), format = "d")
      ) %>%
      inner_join(JSON_area_har, by = c("residence_place" = "id"))
  } else if(input$INFOQtype == "center"){
    consultation_har_filter(JSON_cons_har,
                            source = input$INFOQcountry,
                            new_attack = input$INFOQnew_attack,
                            diagn = input$INFOQdiagn,
                            sex = input$INFOQsex
    ) %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      select(id_center, 
             residence_place, 
             infection_place, 
             patient_age,
             patient_sex,
             diagnosis_result,
             new_attack,
             active_diagnosis
      ) %>%
      group_by(id_center) %>%
      dplyr::summarise(
        count = n(), 
        residence_place = percent(mean(is.na(residence_place)), format = "d"),
        infection_place = percent(mean(is.na(infection_place)), format = "d"),
        patient_age = percent(mean(is.na(patient_age)), format = "d"),
        sex = percent(mean(is.na(patient_sex)), format = "d"),
        diagnosis_result = percent(mean(is.na(diagnosis_result)), format = "d"),
        new_attack = percent(mean(is.na(new_attack)), format = "d"),
        active_diagnosis = percent(mean(is.na(active_diagnosis)), format = "d")
      ) %>%
      inner_join(JSON_healthcenter_har, by = c("id_center" = "id_center"))
  } else if(input$INFOQtype == "infection_place"){
    consultation_har_filter(JSON_cons_har,
                            source = input$INFOQcountry,
                            new_attack = input$INFOQnew_attack,
                            diagn = input$INFOQdiagn,
                            sex = input$INFOQsex
    ) %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      select(infection_place,
             residence_place, 
             id_center, 
             patient_age,
             patient_sex,
             diagnosis_result,
             new_attack,
             active_diagnosis
      ) %>%
      group_by(infection_place) %>%
      dplyr::summarise(
        count = n(), 
        residence_place = percent(mean(is.na(residence_place)), format = "d"),
        center = percent(mean(is.na(id_center)), format = "d"),
        patient_age = percent(mean(is.na(patient_age)), format = "d"),
        sex = percent(mean(is.na(patient_sex)), format = "d"),
        diagnosis_result = percent(mean(is.na(diagnosis_result)), format = "d"),
        new_attack = percent(mean(is.na(new_attack)), format = "d"),
        active_diagnosis = percent(mean(is.na(active_diagnosis)), format = "d")
      ) %>%
      inner_join(JSON_area_har, by = c("infection_place" = "id"))
  }
  
  
})

##################
### Plot
##################

output$INFOQ_plot_sex <- renderPlotly({
  p <- plot_ly(data = INFOQ_plot_tab_fg(), x = ~year, y = ~sex, type = 'scatter', mode = 'lines', name = tr("FG")) %>%
    add_trace(data = INFOQ_plot_tab_br(), x = ~year, y = ~sex, type = 'scatter', mode = 'lines', name = tr("BR")) %>%
    layout(xaxis = list(title = tr("year")), yaxis = list(title = "%", range = c(0,100)))
  p
})
output$INFOQ_plot_residence_area <- renderPlotly({
  p <- plot_ly(data = INFOQ_plot_tab_fg(), x = ~year, y = ~residence_place, type = 'scatter', mode = 'lines', name = tr("FG")) %>%
    add_trace(data = INFOQ_plot_tab_br(), x = ~year, y = ~residence_place, type = 'scatter', mode = 'lines', name = tr("BR")) %>%
    layout(xaxis = list(title = tr("year")), yaxis = list(title = "%", range = c(0,100))) 
  p
})
output$INFOQ_plot_infection_place <- renderPlotly({
  p <- plot_ly(data = INFOQ_plot_tab_fg(), x = ~year, y = ~infection_place, type = 'scatter', mode = 'lines', name = tr("FG")) %>%
    add_trace(data = INFOQ_plot_tab_br(), x = ~year, y = ~infection_place, type = 'scatter', mode = 'lines', name = tr("BR")) %>%
    layout(xaxis = list(title = tr("year")), yaxis = list(title = "%", range = c(0,100)))
  p
})
output$INFOQ_plot_center <- renderPlotly({
  p <- plot_ly(data = INFOQ_plot_tab_fg(), x = ~year, y = ~center, type = 'scatter', mode = 'lines', name = tr("FG")) %>%
    add_trace(data = INFOQ_plot_tab_br(), x = ~year, y = ~center, type = 'scatter', mode = 'lines', name = tr("BR")) %>%
    layout(xaxis = list(title = tr("year")), yaxis = list(title = "%", range = c(0,100)))
  p
})

##################
### Table
##################

output$INFOQ_ftable <- renderDataTable({
  if(input$INFOQtype == "residence_area"){
    INFOQ_tab() %>%
      plyr::arrange(-count) %>% 
      select(
        name, 
        count, 
        source, 
        center, 
        infection_place, 
        patient_age, 
        sex, 
        diagnosis_result,
        new_attack,
        active_diagnosis
      ) %>%
      mutate(source = ifelse(source == "BR", tr("BR"), tr("FG"))) %>%
      formattable(., list(
        center = color_tile("transparent", "orange"),
        infection_place = color_tile("transparent", "orange"),
        patient_age = color_tile("transparent", "orange"),
        sex = color_tile("transparent", "orange"),
        diagnosis_result = color_tile("transparent", "orange"),
        new_attack = color_tile("transparent", "orange"),
        active_diagnosis = color_tile("transparent", "orange")
      )) %>%
      as.datatable(., colnames = c(
        tr("name"),
        tr("cases"), 
        tr("country"),
        tr("center"),
        tr("infection_place"),
        tr("age"),
        tr("sex"),
        tr("diagnosis_result"),
        tr("new_attack"),
        tr("active_search_leg")
      ), 
      extensions = 'Buttons', options = list(
        dom = 'Bfrtip',
        buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
      )
    )
  } else if(input$INFOQtype == "center"){
    INFOQ_tab() %>%
      plyr::arrange(-count) %>% 
      select(
        name, 
        count, 
        source, 
        residence_place, 
        infection_place, 
        patient_age, 
        sex, 
        diagnosis_result,
        new_attack,
        active_diagnosis
      ) %>%
      mutate(source = ifelse(source == "BR", tr("BR"), tr("FG"))) %>%
      formattable(., list(
        residence_place = color_tile("transparent", "orange"),
        infection_place = color_tile("transparent", "orange"),
        patient_age = color_tile("transparent", "orange"),
        sex = color_tile("transparent", "orange"),
        diagnosis_result = color_tile("transparent", "orange"),
        new_attack = color_tile("transparent", "orange"),
        active_diagnosis = color_tile("transparent", "orange")
      )) %>%
      as.datatable(., colnames = c(
        tr("name"),
        tr("cases"), 
        tr("country"),
        tr("residence_area"),
        tr("infection_place"),
        tr("age"),
        tr("sex"),
        tr("diagnosis_result"),
        tr("new_attack"),
        tr("active_search_leg")
      ), 
      extensions = 'Buttons', options = list(
        dom = 'Bfrtip',
        buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
      )
    )
  } else if(input$INFOQtype == "infection_place"){
    INFOQ_tab() %>%
      plyr::arrange(-count) %>% 
      select(
        name, 
        count, 
        source, 
        residence_place, 
        center, 
        patient_age, 
        sex, 
        diagnosis_result,
        new_attack,
        active_diagnosis
      ) %>%
      mutate(source = ifelse(source == "BR", tr("BR"), tr("FG"))) %>%
      formattable(., list(
        residence_place = color_tile("transparent", "orange"),
        center = color_tile("transparent", "orange"),
        patient_age = color_tile("transparent", "orange"),
        sex = color_tile("transparent", "orange"),
        diagnosis_result = color_tile("transparent", "orange"),
        new_attack = color_tile("transparent", "orange"),
        active_diagnosis = color_tile("transparent", "orange")
      )) %>%
      as.datatable(., colnames = c(
        tr("name"),
        tr("cases"), 
        tr("country"),
        tr("residence_area"),
        tr("center"),
        tr("age"),
        tr("sex"),
        tr("diagnosis_result"),
        tr("new_attack"),
        tr("active_search_leg")
      ),
      extensions = 'Buttons', options = list(
        dom = 'Bfrtip',
        buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
      )
    )
  }
},
server = FALSE
)