R/import_bnvd.R

Defines functions import_bnvd

Documented in import_bnvd

#' Import and prepare BNVD data
#'
#' @import dplyr
#' 
import_bnvd <- function(file_list) {

    download_bnvd <- function(url) {

        tempCSV = tempfile(fileext = ".csv")

        old.timeout <- options()$timeout
        options(timeout = 40000)
        utils::download.file(url, destfile = tempCSV,
                      method = "libcurl", cacheOK = TRUE)
        options(timeout = old.timeout)

        data <- data.table::fread(tempCSV)

        file.remove(tempCSV)

        return(data)
    }

    purrr::map(.x = file_list,
        .f = download_bnvd) %>%
        bind_rows() %>%
        group_by(departement, annee, substance, cas) %>%
        summarise(quantite = sum(quantite_substance)) %>%
        ungroup() %>%
        mutate(departement = case_when(departement == "TERRITOIRE DE BELFORT" ~
                                           "TERRITOIRE-DE-BELFORT",
                                       TRUE ~ departement))
}
CedricMondy/afbBNVD documentation built on May 8, 2019, 9:53 p.m.