Skip to content
Snippets Groups Projects
mapBKP.R 4.17 KiB
Newer Older
Vincent DUPONT's avatar
Vincent DUPONT committed
### Coords information changes
# coords <- coords %>%
#   select(name, id_sivep, id_cdps, X, Y) %>%
#   mutate(id_sivep = as.character(id_sivep)) %>%
#   mutate(id_cdps = as.character(id_cdps)) %>%
#   mutate(id_sivep = str_replace_all(id_sivep, "[\r\n]" , "")) %>%
#   mutate(id_cdps = str_replace_all(id_cdps, "[\r\n]" , "")) %>%
#   mutate(X = as.numeric(as.character(X)), Y = as.numeric(as.character(Y))) %>%
#   mutate(name = as.character(name)) %>%
#   mutate(id = if_else(is.na(id_sivep), id_cdps, id_sivep)) %>%
#   mutate(y_coordinate = Y, x_coordinate = X) %>%
#   select(id, name, x_coordinate, y_coordinate)



### Map tab

output$map <- renderLeaflet({
  # First load populate
  date1 <- as.Date("2007-01-01")
  date2 <- as.Date(today())
  df <- JSON_cons_har %>%
    filter(consultation_date >= date1 & consultation_date <= date2) %>%
    select(residence_place, source) %>%
    group_by(residence_place, source) %>%
    dplyr::summarise(count=n()) %>%
    inner_join(coords, by = c("residence_place" = "id")) %>%
    mutate(id = residence_place)
  
  pal <- colorFactor(c("red","blue"), df$source)
  
  leaflet() %>%
    addTiles(options = providerTileOptions(maxZoom = 10)) %>%
    setView(lng = -51.548, lat = 3.938, zoom = 9) %>%
    addCircleMarkers(
      layerId = ~id,
      data = df[which(df$source == "FR-GF"),],
      lat = ~y_coordinate,
      lng = ~x_coordinate,
      radius = ~log(count+1)*1.5,
      color = ~pal(source),
      stroke = FALSE, fillOpacity = 0.5,
      label = ~paste0(as.character(name), " (", source, ")", ": ", count)
    ) %>% 
    addCircleMarkers(
      layerId = ~id,
      data = df[which(df$source == "BR"),],
      lat = ~y_coordinate,
      lng = ~x_coordinate,
      radius = ~log(count+1)*1.5,
      color = ~pal(source),
      stroke = FALSE, fillOpacity = 0.5,
      label = ~paste0(as.character(name), " (", source, ")", ": ", count)
    ) %>%
    addLegend("bottomleft", pal = pal, values = df$source, layerId = "mapLegend") 
})

output$MAPmissing <- renderText("")

# Interactive populate
observe({
  # Input requirements
  req(input$MAPdates)
  req(input$MAPtype)
  
  date1 <- as.Date(paste0(input$MAPdates[1],"-01-01"))
  date2 <- as.Date(paste0(input$MAPdates[2],"-12-31"))
  map_type <- input$MAPtype


  if(map_type == "residence_area"){
    df <- JSON_cons_har %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      select(residence_place, source) %>%
      group_by(residence_place, source) %>%
      dplyr::summarise(count=n()) %>%
      inner_join(coords, by = c("residence_place" = "id")) %>%
      mutate(id = residence_place)
    
    missing_total <- JSON_cons_har %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      nrow()
    
    missing_per <- round((sum(df$count)/missing_total)*100, 2)
    missing_n <- sum(df$count)
  } else if(map_type == "infection_place"){
    df <- JSON_cons_har %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      select(infection_place, source) %>%
      group_by(infection_place, source) %>%
      dplyr::summarise(count=n()) %>%
      inner_join(coords, by = c("infection_place" = "id")) %>%
      mutate(id = infection_place)
    
    missing_total <- JSON_cons_har %>%
      filter(consultation_date >= date1 & consultation_date <= date2) %>%
      nrow()
    
    missing_per <- round((sum(df$count)/missing_total)*100, 2)
    missing_n <- sum(df$count)
  }

  # Map
  pal <- colorFactor(c("red","blue"), df$source)
  leafletProxy("map") %>%
    clearMarkers() %>%
    addCircleMarkers(
      layerId = ~id,
      data = df,
      lat = ~y_coordinate,
      lng = ~x_coordinate,
      radius = ~log(count+1)*1.5,
      color = ~pal(df$source),
      stroke = FALSE, fillOpacity = 0.5,
      label = ~paste0(as.character(name), " (", source, ")", ": ", count)
    ) %>% addLegend("bottomleft", pal = pal, values = df$source, layerId = "mapLegend")

  #output$MAPmissing <- renderText(paste0("De ", missing_total, "registros, foram localizados no mapa ", missing_n, " (", missing_per, "%)."))
  output$MAPmissing <- renderText(paste0(tr("missing_1"), " ", missing_total," ", tr("missing_2"), ", ", missing_n, " (", missing_per, "%) ", tr("missing_3"), "."))
  
  })