R/prep_packageData.R

Defines functions prep_packageData

Documented in prep_packageData

#' Prepare the data used in the package
#'
#' This function should only be used to update the internal data of the package
#' before building a new version. It is included to show how raw data were
#' processed before being included in the Shiny app.
#'
#' @import dplyr
#' 
prep_packageData <- function() {
    ## Date de l'export des données ----
    date_export <- Sys.Date()

    ## Liste des substances ----
    correspondance_cas_sandre <-
        readr::read_csv2(system.file("extdata",
                              "afb_amm_substance_cas_code_sandre.csv",
                              package = "afbBNVD")) %>%
        select(substance, code_sandre) %>%
        distinct()

    # Le fichier GPR_XXXXXXXX_SANDRE.csv.gz est à télécharger avec le lien suivant:
    # http://www.sandre.eaufrance.fr/atlas/srv/fre/catalog.search#/metadata/43e37773-5969-44b7-9031-2ff159dcc460

    bnvd_substances <-
        import_subst(fileSandre = system.file("extdata",
                                              "GPR_20190312_SANDRE.csv.gz",
                                              package = "afbBNVD"),
                     fileCAS = system.file("extdata",
                                           "afb_amm_substance_cas_code_sandre.csv",
                                           package = "afbBNVD"))

    ## Import des données -----

    # BNVD
    bnvd_url <-
        list(BNVD_2018_VENTE_SUBSTANCE_2008 = "https://www.data.gouv.fr/fr/datasets/r/3b63d103-330e-41b0-babe-1a890631e214",
             BNVD_2018_VENTE_SUBSTANCE_2009 = "https://www.data.gouv.fr/fr/datasets/r/d08e5842-abd5-457e-8eab-d94c9e4a6214",
             BNVD_2018_VENTE_SUBSTANCE_2010 = "https://www.data.gouv.fr/fr/datasets/r/0cfa3bd8-c4bd-407b-8cef-9f8498ccae4a",
             BNVD_2018_VENTE_SUBSTANCE_2011 = "https://www.data.gouv.fr/fr/datasets/r/6fef8cf7-e2f9-4089-b4ce-12a62572334d",
             BNVD_2018_VENTE_SUBSTANCE_2012 = "https://www.data.gouv.fr/fr/datasets/r/3b732693-8d19-43d5-8675-961bed6e607a",
             BNVD_2018_VENTE_SUBSTANCE_2013 = "https://www.data.gouv.fr/fr/datasets/r/64451253-ed0b-4262-a8fa-655f836089fe",
             BNVD_2018_VENTE_SUBSTANCE_2014 = "https://www.data.gouv.fr/fr/datasets/r/7e4d5850-5093-4529-92a7-a25966468b18",
             BNVD_2018_VENTE_SUBSTANCE_2015 = "https://www.data.gouv.fr/fr/datasets/r/dbd0bb7a-0234-4f46-85a1-7d0ff208dd6d",
             BNVD_2018_VENTE_SUBSTANCE_2016 = "https://www.data.gouv.fr/fr/datasets/r/30dd11ff-601d-4a74-b147-9f1b84a8d08b",
             BNVD_2018_VENTE_SUBSTANCE_2017 = "https://www.data.gouv.fr/fr/datasets/r/4a8764ee-4e38-4c92-9741-76c0cc88e96e"
        )

    bnvd_data <- import_bnvd(bnvd_url) %>%
        filter(substance %in% bnvd_substances)

    bnvd_meta <-
        tibble(
            column = colnames(bnvd_data),
            description =
                c(
                    departement = "Département du point de vente",
                    annee = "Année de la vente",
                    substance = "Libellé de la substance active contenue dans le produit phytopharmaceutique",
                    cas = "Numéro CAS de la substance active contenue dans le produit phytopharmaceutique",
                    quantite = "Quantité de substance active vendue exprimée en kilogrammes avec un nombre variable de chiffres significatifs"))

    attr(bnvd_data, "variable.labels") <- bnvd_meta$description

    # Limites des départements (Shapefile à télécharger à l'adresse suivante:
    # https://www.data.gouv.fr/fr/datasets/r/eb36371a-761d-44a8-93ec-3d728bec17ce
    dept_file <- system.file("extdata",
                             "departements-20180101.shp",
                             package = "afbBNVD")
    dept_data <- import_dept(file = dept_file)

    ## Préparation des données ----

    sale_trends <- prep_tendances(bnvd_data, dept_data)
    
    # Données spatialisées
    filter(bnvd_data, annee == max(annee)) %>%
        prep_ventes() %>%
        full_join(.,
                  sf::st_centroid(dept_data),
                  by = c("departement" = "nom")) %>%
        left_join(y = sale_trends, by = "departement") %>% 
    sf::st_as_sf() %>% 
        filter(!is.na(quantite)) %>% 
        select(departement, quantite, pente, pente_trunc) %>% 
    sf::st_write(., "inst/extdata/dernieres_ventes.gpkg",
                 delete_layer = TRUE)

    # Substances représentant plus de 50% des ventes
    top_substances <- prep_top_substances(bnvd_data, prop = 0.5) %>%
        left_join(correspondance_cas_sandre, by = "substance")

    # Tendances
    tendances <- prep_data_tendances(bnvd_data, top_substances) %>%
        select(-cas)

    usethis::use_data(date_export,
         top_substances, tendances,
         internal = FALSE, overwrite = TRUE)

}
CedricMondy/afbBNVD documentation built on May 8, 2019, 9:53 p.m.