R/utils_etl.R

Defines functions prep_feries prep_current prep_historic dwn_bikecount_files

# Cette fonction télécharge els fichiers sources à partir du portail 
# open data de Nantes Métropole
dwn_bikecount_files <- function(current = FALSE, historic = FALSE, location = FALSE) {
  if (current) {
    download.file("https://data.nantesmetropole.fr/explore/dataset/244400404_comptages-velo-nantes-metropole/download/?format=csv&timezone=Europe/Berlin&lang=fr&use_labels_for_header=true&csv_separator=%3B",
                  destfile = "bikecount_currentyear.csv")
  } 
  if (historic) {
    download.file("https://data.nantesmetropole.fr/explore/dataset/244400404_comptages-velo-nantes-metropole-historique-jour/download/?format=csv&timezone=Europe/Berlin&lang=fr&use_labels_for_header=true&csv_separator=%3B",
                  destfile = "bikecount_historic.csv")
  } 
  if (location) {
    download.file("https://data.nantesmetropole.fr/explore/dataset/244400404_comptages-velo-nantes-metropole-boucles-comptage/download/?format=csv&timezone=Europe/Berlin&lang=fr&use_labels_for_header=true&csv_separator=%3B",
                  destfile = "bikecount_location.csv")
  }
}


prep_historic <- function(hist_file = "bikecount_historic.csv") {
  read_delim(hist_file,
             delim = ";", skip = 1,
             col_names = c("id", "date", "name", "count", "anom", "fitted" ))
}


prep_current <- function(current_file = "bikecount_currentyear.csv") {
  read_delim(current_file,
             delim = ";", skip = 1,
             col_names = c("id", "name", "date", 0:23, "anom", "weekday" )) %>%
    mutate(name = ifelse(is.na(name), paste0("Sans nom : ", id), name),
           anom = ifelse(is.na(anom), "fonctionnel", anom),
           anom = recode_factor(anom,
                                fonctionnel = "Fonctionnel",
                                Faible = "Faible probabilité d'anomalie",
                                Forte = "Forte probabilité d'anomalie",
                                .ordered = TRUE))
}

prep_feries <- function(file_holidays = "jours_feries.csv") {
  jours_feries <- read_csv(file_holidays) %>%
    filter(annee %in% 2014:2022) %>%
    select(ds = date, holiday = nom_jour_ferie)
}
fBedecarrats/bikeMonitoR documentation built on Jan. 1, 2021, 1:16 a.m.