R/mod_carto.R

Defines functions mod_carto_ui mod_carto_server

Documented in mod_carto_server mod_carto_ui

# Module UI

#' @title   mod_carto_ui and mod_carto_server
#' @description  Module des cartes
#'
#' @param id shiny id
#' @param input internal
#' @param output internal
#' @param session internal
#'
#' @rdname mod_carto
#'
#' @keywords internal
#' @import dplyr
#' @import tidyr
#' @import forcats
#' @export
#' @importFrom shiny NS tagList
mod_carto_ui <- function(id) {
  ns <- NS(id)
  tagList(tagList(plotOutput(ns("carto"))))
}

# Module Server

#' @rdname mod_carto
#' @export
#' @keywords internal

mod_carto_server <-
  function(input,
           output,
           session,
           r,
           type_ind = c("ind_tension_all_d",
                        "ind_int_emb_all_d",
                        "ind_chomage_d",
                        "ind_dur_empl_all_d")) {
    ns <- session$ns
    
    output$carto <- renderPlot({
      df_dep_filtre <- data_tensions_dep_fap %>%
        filter(fap87 == r$fap87_filtre) %>%
        pivot_longer(
          cols = c(
            ind_tension_all_d,
            ind_int_emb_all_d,
            ind_chomage_d,
            ind_dur_empl_all_d
          ),
          names_to = "type_indicateur",
          values_to = "val_indicateur"
        ) %>%
        filter(type_indicateur == type_ind)
      
      shp_dep_filtre <- shp_dep %>%
        left_join(df_dep_filtre , by = c("code_dep" = "dep"))
      
      # cartes
      ggplot(shp_dep_filtre) +
        geom_sf() +
        stat_sf_coordinates(
          aes(size = emploi_moyen,
              fill = val_indicateur),
          shape = 21,
          color = "grey60"
        ) +
        scale_size_continuous(name = "Emploi moyen",
                              range = c(1, 12),
                              guide = guide_legend(order = 0)) +
        scale_fill_manual(
          name = "Valeur de \nl'indicateur",
          values = c(
            "1" = "seagreen",
            "2" = "lightgreen",
            "3" = "yellow2",
            "4" = "orange1",
            "5" = "tomato"
          ),
          labels = c(
            "1" = "Très faible",
            "2" = "Faible",
            "3" = "Moyen",
            "4" = "Fort",
            "5" = "Très fort"
          ),
          na.value = "grey",
          drop = FALSE,
          guide = guide_legend(override.aes = list(size = 10),
                               order = 1)
        ) +
        theme_void() +
        theme(
          panel.grid = element_line(NULL),
          text = element_text(color = "#636362"),
          legend.title = element_text(size = 12, face = "bold"),
          legend.text = element_text(size = 12)
        )
    }, width = 600, height = 500)
  }

## To be copied in the UI
# mod_carto_ui("carto_ui_1")

## To be copied in the server
# callModule(mod_carto_server, "carto_ui_1")
tvroylandt/gravitype documentation built on Feb. 7, 2020, 2:37 a.m.