---
title: "dev_history.Rmd pour le package bilan.eco.insee"
author: "Juliette Engelaere-Lefebvre"
date: "15/03/2021 (actualis\u00e9 : `r format(Sys.Date(), '%d/%m/%Y')`)"
output: html_document
params:
  annee: "2020"
editor_options: 
  chunk_output_type: console
---

```{r development, include=FALSE}
library(testthat)
library(dplyr)
library(httr)
library(jsonlite)
library(tibble)
library(readr)
```

<!--
# Description of your package

This will fill the description of your package.
Add this chunk using `fusen::description_chunk()`
--> 

```{r description}
# Describe your package
fusen::fill_description(
  pkg = here::here(),
  fields = list(
    Title = "Bilan economique et social regional INSEE ",
    Description = "Collecte, datapreparation et datavisualiation des donnees energie necessaires \u00e0 la page energie/climat/air du bilan economique et social regional de l\'INSEE Pays de la Loire.",
    `Authors@R` = c(
      person("Juliette", "Engelaere-Lefebvre", email = "Juliette.Engelaere@developpement-durable.gouv.fr", role = c("aut", "cre")),
      person(given = "DREAL Pays de la Loire", role = "cph")
    )
  )
)
# Define License with use_*_license()
usethis::use_mit_license("Juliette Engelaere-Lefebvre")
# Create "inst/" directory
dir.create(here::here("inst"))
usethis::use_pipe()
```

# Fonctions

## Téléchargement des données depuis la plate-forme ODRE

```{r function}
#' Telechargement dataset ODRE
#'
#' @param x nom du jeu de donnee
#'
#' @return un dataframe correspondant au dataset lu
#' @importFrom dplyr left_join pull
#' @importFrom httr GET content
#' @importFrom jsonlite fromJSON
#' @importFrom readr read_csv2 locale
#' @importFrom tibble enframe
#' @importFrom rlang .data
#' 
#' @export
#'
#' @examples
dataset_odre <- function(x = "prod-region-annuelle-enr") {
  baseurl  <- paste0("https://opendata.reseaux-energies.fr/api/dataset/1.0/", x)
  csvurl <- paste0("https://opendata.reseaux-energies.fr/explore/dataset/", x,
                   "/download/?format=csv&timezone=Europe/Berlin&lang=fr&use_labels_for_header=true&csv_separator=%3B")
  
  metadata <- httr::GET(baseurl) %>%
    httr::content("text") %>%
    jsonlite::fromJSON(flatten = TRUE)
  
  if (!is.null(metadata$error)) {stop(paste0("le dataset ", x, " n\'existe pas"))}
  
  attributs <- metadata$fields
  dataset <- readr::read_csv2(file = csvurl, locale = readr::locale(decimal_mark = ".")) %>% 
    as.data.frame()
  
  names(dataset) %>% tibble::enframe(name = "n_col", value = "label") %>% dplyr::left_join(attributs, by = "label") %>% dplyr::pull(.data$name) -> names(dataset)
  
  return(list(dico_var = attributs, dataset = dataset))
}
```

```{r examples}
prod_enr <- dataset_odre(x = "prod-region-annuelle-enr")
prod_enr
```

```{r tests}
testthat::test_that("dataset_odre fonctionne correctement et renvoie une error si le dataset n\'existe pas", {
  obj <- dataset_odre(x = "prod-region-annuelle-enr")
  
  testthat::expect_true(is.list(obj))
  testthat::expect_true(is.data.frame(obj$dataset))
  testthat::expect_true(is.data.frame(obj$dico_var))

  testthat::expect_error(dataset_odre(x = "prod-region-annuelle-enrrrr"))
})
```


# There can be development actions

These will be included in the `dev_history.R` file of your package, but won't be direct part of it.

```{r development-1, eval=FALSE}
# Run but keep eval=FALSE to avoid infinite loop
# Execute in the console directly
fusen::inflate(rmd = "dev/dev_history.Rmd")
```


# Inflate your package

You're one inflate from paper to box.
Build your package from this very Rmarkdown using `fusen::inflate()`

- Verify your `"DESCRIPTION"` file has been updated
- Verify your function is in `"R/"` directory
- Verify your test is in `"tests/testthat/"` directory
- Verify this Rmd appears in `"vignettes/"` directory


```{r suite dev}

usethis::use_r("prep_dju")
usethis::use_r("prep_conso")
usethis::use_r("finit_conso")
usethis::use_r("viz_conso")
usethis::use_r("table_conso")
usethis::use_test("conso")

usethis::use_r("prep_prod")
usethis::use_test("prod")
usethis::use_data_raw("filiere")
usethis::use_data_raw("regions")
utilitaires.ju::use_data_doc(name = "filieres", description = "Libelle des filiere du dataset ODRE 'prod-region-annuelle-enr'.", source = "ODRE")
utilitaires.ju::use_data_doc(name = "regions", description = "codes et libelles des regions francaises")
usethis::use_r("viz_prod")
usethis::use_r("table_prod")
usethis::use_r("table_biogaz")
usethis::use_r("globals")
usethis::use_r("edit_rapport")
usethis::use_test("edit_rapport")


usethis::use_package("readxl")
usethis::use_package("tidyr")
usethis::use_package("tricky")
usethis::use_package("rlang")
usethis::use_package("flextable")
usethis::use_package("ggplot2")
usethis::use_package("gouvdown")
usethis::use_package("officedown")
usethis::use_package("officer")
usethis::use_package("lubridate")
usethis::use_package("forcats")
usethis::use_package("knitr")
usethis::use_package("flextable")
usethis::use_package("tidyselect")
usethis::use_package("curl")
usethis::use_package("scales")
usethis::use_package("usethis")
usethis::use_package("utils")
# usethis::use_dev_package("COGiter", type = "Imports", remote = "MaelTheuliere/COGiter")
usethis::use_package("tricky")
usethis::use_package("writexl", type = "Suggests")

usethis::use_pkgdown()
# Run to build the website
pkgdown::build_site()
usethis::use_gitlab_ci()




```

