diff --git a/.Rbuildignore b/.Rbuildignore index a5d77f856db6a9e04fa31efff9ea1107d0ddda9f..c726cfaf36aca9dce8729bc6c61ae847e9f60a42 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1 +1,6 @@ ^dev$ +^LICENSE\.md$ +^README\.Rmd$ +^CODE_OF_CONDUCT\.md$ +^data\.nitrates\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore index 507e045ab5af5b8ed416b5ba33291704b2ef3e32..000e2e4d8be5c846dc013dadb7b93287d11dcd07 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .RData .DS_Store .httr-oauth +inst/doc diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000000000000000000000000000000000..85d5cd6409100569d8e285eff6cbb806bed2503d --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,126 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, + without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at ronan.vignard@developpement-durable.gouv.fr. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +<https://www.contributor-covenant.org/version/2/1/code_of_conduct.html>. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][https://github.com/mozilla/inclusion]. + +For answers to common questions about this code of conduct, see the FAQ at +<https://www.contributor-covenant.org/faq>. Translations are available at <https://www.contributor-covenant.org/translations>. + +[homepage]: https://www.contributor-covenant.org diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000000000000000000000000000000000000..19e43c2735f2b9373ac93322c9952ac0c1740a0b --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,19 @@ +Package: data.nitrates +Title: Collecte Des Données Sur Les Nitrates +Version: 0.0.0.9000 +Authors@R: + person("Ronan", "Vignard", , "ronan.vignard@developpement-durable.gouv.fr", role = c("aut", "cre"), + comment = c(ORCID = "0000-0000-0000-0000")) +Description: Import des données nitrates à partir des sources ARS et + Hub'eau dans une base de données PostgreSQL. +License: MIT + file LICENSE +Suggests: + collectr, + knitr, + rmarkdown +VignetteBuilder: + knitr +Config/fusen/version: 0.5.2 +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..6921b4cc59d464d0e6b2279207c0db0ada8e1622 --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2024 +COPYRIGHT HOLDER: Ronan Vignard diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..b2cd6e60b9634bd90b8599f8079941f90e2968e4 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright (c) 2024 Ronan Vignard + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000000000000000000000000000000000000..6ae926839dd1829f1016a96f766d970ff184ad97 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,2 @@ +# Generated by roxygen2: do not edit by hand + diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000000000000000000000000000000000000..8ab587f3feff8f00a33fbca1788469b10572de03 --- /dev/null +++ b/NEWS.md @@ -0,0 +1,3 @@ +# data.nitrates (development version) + +* Initial CRAN submission. diff --git a/README.Rmd b/README.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..96d24b6d854d1e134ff5b87ceecd12248f144c7a --- /dev/null +++ b/README.Rmd @@ -0,0 +1,69 @@ +--- +output: github_document +--- + +<!-- README.md is generated from README.Rmd. Please edit that file --> + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + fig.path = "man/figures/README-", + out.width = "100%" +) +``` + +# data.nitrates + +<!-- badges: start --> +[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/eau-milieux-aquatiques/data.nitrates/-/releases) +[](https://opensource.org/licenses/MIT) +[](https://lifecycle.r-lib.org/articles/stages.html#experimental) +[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/eau-milieux-aquatiques/data.nitrates/-/commits/master) + +<!-- badges: end --> + +## Objectif + +L'objectif du package `data.nitrates` est de faciliter l'actualisation des lots de données suivants dans une base de données PostgreSQL locale. + +**Sources :** + +- Données de l'ARS : `nitrates.nitrate_data_analyse_ars` +- Données provenant de la plateforme Hub'eau : + - [API "Qualité des nappes d'eau souterraine"](https://hubeau.eaufrance.fr/page/api-qualite-nappes) : `qualite_nappes_eau_souterraine.nitrate_qualite_nappes_analyses` + - [API "Qualité des cours d'eau"](https://hubeau.eaufrance.fr/page/api-qualite-cours-deau) : `qualite_cours_d_eau.hubeau_qualite_rivieres_station_pc` + +**Livrables :** + +- Table des prélèvements : `nitrates.nitrate_prelevement` +- Table des analyses : `nitrates.nitrate_analyse` + +## Installation + +Installer le package `remotes` si besoin : + +```{r install-remotes, eval=FALSE} +install.packages("remotes") +``` + + +Installer le package `data.nitrates` : + +```{r install-package, eval=FALSE} +remotes::install_gitlab('dreal-pdl/csd/eau-milieux-aquatiques/data.nitrate', host="gitlab-forge.din.developpement-durable.gouv.fr") +``` + +## Utilisation + +### Chargement du package +Charger le package `data.nitrates` dans la session : + +```{r load-package, eval=FALSE} +library(data.nitrates) +``` + +### Documentation + +La documentation du package est consultable sur ce site : +https://dreal-pdl.gitlab-pages.din.developpement-durable.gouv.fr/csd/eau-milieux-aquatiques/data.nitrate/ diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b99b734368f8b18511ccb6cbc159f491f18a0332 --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ + +<!-- README.md is generated from README.Rmd. Please edit that file --> + +# data.nitrates + +<!-- badges: start --> + +[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/eau-milieux-aquatiques/data.nitrates/-/releases) +[](https://opensource.org/licenses/MIT) +[](https://lifecycle.r-lib.org/articles/stages.html#experimental) +[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/eau-milieux-aquatiques/data.nitrates/-/commits/master) + +<!-- badges: end --> + +## Objectif + +L’objectif du package `data.nitrates` est de faciliter l’actualisation +des lots de données suivants dans une base de données PostgreSQL locale. + +**Sources :** + +- Données de l’ARS : `nitrates.nitrate_data_analyse_ars` +- Données provenant de la plateforme Hub’eau : + - [API “Qualité des nappes d’eau + souterraine”](https://hubeau.eaufrance.fr/page/api-qualite-nappes) : + `qualite_nappes_eau_souterraine.nitrate_qualite_nappes_analyses` + - [API “Qualité des cours + d’eau”](https://hubeau.eaufrance.fr/page/api-qualite-cours-deau) : + `qualite_cours_d_eau.hubeau_qualite_rivieres_station_pc` + +**Livrables :** + +- Table des prélèvements : `nitrates.nitrate_prelevement` +- Table des analyses : `nitrates.nitrate_analyse` + +## Installation + +Installer le package `remotes` si besoin : + +``` r +install.packages("remotes") +``` + +Installer le package `data.nitrates` : + +``` r +remotes::install_gitlab('dreal-pdl/csd/eau-milieux-aquatiques/data.nitrate', host="gitlab-forge.din.developpement-durable.gouv.fr") +``` + +## Utilisation + +### Chargement du package + +Charger le package `data.nitrates` dans la session : + +``` r +library(data.nitrates) +``` + +### Documentation + +La documentation du package est consultable sur ce site : +<https://dreal-pdl.gitlab-pages.din.developpement-durable.gouv.fr/csd/eau-milieux-aquatiques/data.nitrate/> diff --git a/data.nitrates.Rproj b/data.nitrates.Rproj index c8daeb91df75db32b7653943a4ee2b557eddad0e..6a3ede2239ad87da09a17c21b42cb9bb5ea83810 100644 --- a/data.nitrates.Rproj +++ b/data.nitrates.Rproj @@ -15,3 +15,7 @@ LaTeX: pdfLaTeX AutoAppendNewline: Yes StripTrailingWhitespace: Yes LineEndingConversion: Posix + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/dev/0-dev_history.Rmd b/dev/0-dev_history.Rmd index 628b333c51f7927ee0bcf2f467de69d77f03bdf1..d8a08b2fc6f207bcb92ec284f2d514034b3e2f37 100644 --- a/dev/0-dev_history.Rmd +++ b/dev/0-dev_history.Rmd @@ -16,15 +16,15 @@ All commands that you use to use when developing packages... fusen::fill_description( pkg = here::here(), fields = list( - Title = "Build A Package From Rmarkdown File", - Description = "Use Rmarkdown First method to build your package. Start your package with documentation. Everything can be set from a Rmarkdown file in your project.", + Title = "Collecte des données sur les nitrates", + Description = "Import des données nitrates à partir des sources ARS et Hub'eau dans une base de données PostgreSQL.", `Authors@R` = c( - person("John", "Doe", email = "john@email.me", role = c("aut", "cre"), comment = c(ORCID = "0000-0000-0000-0000")) + person("Ronan", "Vignard", email = "ronan.vignard@developpement-durable.gouv.fr", role = c("aut", "cre"), comment = c(ORCID = "0000-0000-0000-0000")) ) ) ) # Define License with use_*_license() -usethis::use_mit_license("John Doe") +usethis::use_mit_license("Ronan Vignard") ``` # All-in-one function to deploy publicly on GitHub @@ -56,7 +56,7 @@ remotes::install_local() # README usethis::use_readme_rmd() # Code of Conduct -usethis::use_code_of_conduct("contact@fake.com") +usethis::use_code_of_conduct("ronan.vignard@developpement-durable.gouv.fr") # NEWS usethis::use_news_md() ``` diff --git a/dev/config_attachment.yaml b/dev/config_attachment.yaml new file mode 100644 index 0000000000000000000000000000000000000000..46e24ecc2173ddbd6d930225a01bb92e508508d5 --- /dev/null +++ b/dev/config_attachment.yaml @@ -0,0 +1,12 @@ +path.n: NAMESPACE +path.d: DESCRIPTION +dir.r: R +dir.v: vignettes +dir.t: tests +extra.suggests: ~ +pkg_ignore: ~ +document: yes +normalize: yes +inside_rmd: no +must.exist: yes +check_if_suggests_is_installed: yes diff --git a/dev/config_fusen.yaml b/dev/config_fusen.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7b7d7547f62d7f7f3ab58a7da562e90667f1185e --- /dev/null +++ b/dev/config_fusen.yaml @@ -0,0 +1,13 @@ +flat_import_data.Rmd: + path: dev/flat_import_data.Rmd + state: active + R: [] + tests: [] + vignettes: vignettes/import-des-donnees.Rmd + inflate: + flat_file: dev/flat_import_data.Rmd + vignette_name: Import des données + open_vignette: true + check: true + document: true + overwrite: ask diff --git a/dev/flat_first.Rmd b/dev/flat_first.Rmd deleted file mode 100644 index f902c01846178e94bc26b3be29944335a6e42a20..0000000000000000000000000000000000000000 --- a/dev/flat_first.Rmd +++ /dev/null @@ -1,206 +0,0 @@ ---- -title: "flat_first.Rmd for working package" -output: html_document -editor_options: - chunk_output_type: console ---- - -<!-- Run this 'development' chunk --> -<!-- Store every call to library() that you need to explore your functions --> - -```{r development, include=FALSE} -library(testthat) -``` - -<!-- - You need to run the 'description' chunk in the '0-dev_history.Rmd' file before continuing your code there. - -If it is the first time you use {fusen}, after 'description', you can directly run the last chunk of the present file with inflate() inside. ---> - -```{r development-load} -# Load already included functions if relevant -pkgload::load_all(export_all = FALSE) -``` - -# Include some data examples in your package - -<!-- - Store your dataset in a directory named "inst/" at the root of your project. - Use it for your tests in this Rmd thanks to `pkgload::load_all()` to make it available -and `system.file()` to read it in your examples. - -- There already is a dataset in the "inst/" directory to be used in the examples below ---> - -```{r development-dataset} -# Run all this chunk in the console directly -# There already is a dataset in the "inst/" directory -# Make the dataset file available to the current Rmd during development -pkgload::load_all(path = here::here(), export_all = FALSE) - -# You will be able to read your example data file in each of your function examples and tests as follows - see chunks below -datafile <- system.file("nyc_squirrels_sample.csv", package = "data.nitrates") -nyc_squirrels <- read.csv(datafile, encoding = "UTF-8") - -nyc_squirrels -``` - - -# The first function of the package: Calculate the median of a vector - -<!-- -Create a chunk for the core of the function - -- The chunk needs to be named `function` at least -- It contains the code of a documented function -- The chunk can also be named `function-my_median` to make it easily -findable in your Rmd -- Let the `@examples` part empty, and use the next `examples` chunk instead to present reproducible examples - -After inflating the template - -- This function code will automatically be added in a new file in the "R/" directory ---> - -```{r function} -#' My median -#' -#' @param x Vector of Numeric values -#' @inheritParams stats::median -#' -#' @return -#' Median of vector x -#' @export -#' -#' @examples -my_median <- function(x, na.rm = TRUE) { - if (!is.numeric(x)) {stop("x should be numeric")} - stats::median(x, na.rm = na.rm) -} -``` - -<!-- -Create a chunk with an example of use for your function - -- The chunk needs to be named `examples` at least -- It contains working examples of your function -- The chunk is better be named `examples-my_median` to be handled -correctly when inflated as a vignette - -After inflating the template - -- This example will automatically be added in the '@examples' part of our function above in the "R/" directory -- This example will automatically be added in the vignette created from this Rmd template ---> - -```{r examples} -my_median(1:12) - -# Example with your dataset in "inst/" -datafile <- system.file("nyc_squirrels_sample.csv", package = "data.nitrates") -nyc_squirrels <- read.csv(datafile, encoding = "UTF-8") -# Apply my function -my_median(nyc_squirrels[,"hectare_squirrel_number"]) -``` - -<!-- -Create a chunk with a test of use for your function - -- The chunk needs to be named `tests` at least -- It contains working tests of your function -- The chunk is better be named `tests-my_median` to be handled -correctly when inflated as a vignette - -After inflating the template - -- This test code will automatically be added in the "tests/testthat/" directory ---> - -```{r tests} -test_that("my_median works properly and show error if needed", { - expect_true(my_median(1:12) == 6.5) - expect_error(my_median("text")) -}) - -# Test with your dataset in "inst/" -datafile <- system.file("nyc_squirrels_sample.csv", package = "data.nitrates") -nyc_squirrels <- read.csv(datafile, encoding = "UTF-8") -# Apply test on my function -test_that("my_median works properly with internal dataset", { - expect_equal(my_median(nyc_squirrels[,"hectare_squirrel_number"]), 3) -}) -``` - -# Calculate the mean of a vector - -<!-- -There can be other functions, examples and tests in your flat template. -Each of them will be inflated in a different file, provided that there is a level-1 or level-2 section title to separate from previous functions. ---> - -## Use sub-functions in the same chunk - -```{r function-my_other_median} -#' My Other median -#' -#' @param x Vector of Numeric values -#' @inheritParams stats::median -#' -#' @return -#' Median of vector x -#' @export -#' -#' @examples -my_other_median <- function(x, na.rm = TRUE) { - if (!is.numeric(x)) {stop("x should be numeric")} - sub_median(x, na.rm =na.rm) -} - -#' Core of the median not exported -#' @param x Vector of Numeric values -#' @inheritParams stats::median -sub_median <- function(x, na.rm = TRUE) { - stats::median(x, na.rm) -} -``` - -```{r examples-my_other_median} -my_other_median(1:12) -``` - -```{r tests-my_other_median} -test_that("my_median works properly and show error if needed", { - expect_true(my_other_median(1:12) == 6.5) - expect_error(my_other_median("text")) -}) -``` - -<!-- -# There can be development actions - -Create a chunk with 'development' actions - -- The chunk needs to be named `development` or `dev` -- It contains functions that are used for package development only -- Note that you may want to store most of these functions in the 0-dev_history.Rmd file - -These are only included in the present flat template file, their content will not be part of the package anywhere else. ---> - -```{r development-inflate, eval=FALSE} -# Keep eval=FALSE to avoid infinite loop in case you hit the knit button -# Execute in the console directly -fusen::inflate(flat_file = "dev/flat_first.Rmd", vignette_name = "Get started") -``` - - -# Inflate your package - -You're one inflate from paper to box. -Build your package from this very Rmd 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 diff --git a/dev/flat_import_data.Rmd b/dev/flat_import_data.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..7716cf58260decd899115f947c522def21f5821e --- /dev/null +++ b/dev/flat_import_data.Rmd @@ -0,0 +1,180 @@ +--- +title: "Import des données" +output: html_document +editor_options: + chunk_output_type: console +--- + +```{r development, include=FALSE} +library(testthat) +library(xlsx) +library(datalibaba) +library(RPostgreSQL) +library(sf) +library(collectr) +library(dplyr) +``` + +```{r development-1} +# Load already included functions +pkgload::load_all(export_all = FALSE) +``` + +# Présentation + +> Cette page contient la logique métier concernant l'import des données +ARS et Hub'eau dans le cadre du projet Nitrates + +# Connexion à la base de données PostgreSQL + +```{r development-connect_to_db, eval=FALSE} +# Se connecter à la base de données PostgreSQL si_eau +connexion <- datalibaba::connect_to_db(db = "si_eau") +``` + +```{r development-list_schemas, eval=FALSE} +# Lister les schémas présents dans la base +schemas_list <- data.frame(datalibaba::list_schemas(connexion)) + +# Renommer la variable unique en "nom_schema" +schemas_list <- schemas_list |> + rename(nom_schema = unique(names(.))) + +# Trier le dataframe par la variable nom_schema +schemas_list <- schemas_list |> + arrange(nom_schema) +``` + +```{r development-list_tables, eval=FALSE} +# Lister les tables présentes dans le schéma spécifié +tables_list <- data.frame(datalibaba::list_tables( + con = connexion, + db = "si_eau", + schema = "qualite_cours_d_eau")) + +# Renommer la variable unique en "nom_table" +tables_list <- tables_list |> + rename(nom_table = names(tables_list)[1]) + +# Trier le dataframe par la variable nom_table +tables_list <- tables_list |> + arrange(nom_table) +``` + +# Import des données de l'ARS + +```{r} +data_ars_2022 <- collectr::import_xlsx( + filepath = "T:\\datalab\\SRNP_DEMA_SI_EAU\\NITRATES\\DONNEES_CLIENT\\SOURCES\\ARS\\2023_06\\Nitrates 2022 pour DREAL EPNT4 2023-05-30.xlsx", + sheet = 1, + row = 2) +``` + +## Première approche : non concluante + +### Vérification de la correspondande de la structure du dataframe avec celle de la table + +```{r development-check_structure_table, eval=FALSE} +collectr::check_structure_table(connexion, + data_ars_2022, + "nitrate_data_analyse_ars_test", + "qualite_cours_d_eau") +``` + +### Adaptation de la structure + +```{r development, eval=FALSE} +data_ars_2022 <- data_ars_2022 |> + select(-coord_x,-coord_y) |> + mutate(eligible_ades = rep(NA, n())) +``` + +### Insertion des données du dataframe dans la table en base + +La fonction insert_into_table() doit être corrigée. + +```{r development-insert_into_table, eval=FALSE} +collectr::insert_into_table(connexion, + data_ars_2022, + "nitrate_data_analyse_ars_test", + "qualite_cours_d_eau" +) +``` + +## Deuxième approche : concluante + +### Création d'une table pour le nouveau millésime + +```{r create-nitrate_data_ars, eval=FALSE} +poster_data(data = data_ars_2022, + table = "nitrate_data_ars_2022", + schema = "qualite_cours_d_eau", + db = "si_eau", + overwrite = TRUE) + +``` + +### Modification du type des champs si besoin + +```{r modify_column_type, eval=FALSE} +# Appeler la fonction pour modifier le type de colonne +collectr::modify_column_type(connexion, + schema = "qualite_cours_d_eau", + table_name = "nitrate_data_ars_2022", + column_name = "plv_date ", + column_type = "date") +``` + +### Insertion des données du nouveau millésime en base dans la table globale + +```{r import_and_merge_tables, eval=FALSE} +import_and_merge_tables(connexion, + source_table = "nitrate_data_ars_2022", + source_schema = "qualite_cours_d_eau", + target_table = "nitrate_data_analyse_ars_test", + target_schema = "qualite_cours_d_eau") + +``` + +# Import des données Hub'eau ESU + +```{r create-nitrate_analyse_esu_2020, eval=FALSE} +nitrate_analyse_esu_2020 <- get_json_data(code_parametre = "1340", + code_region = "52", + date_debut_prelevement = "2020-01-01", + date_fin_prelevement = "2020-12-31", + size = "5000") +``` + +```{r create-nitrate_analyse_esu_2021, eval=FALSE} +nitrate_analyse_esu_2021 <- get_json_data(code_parametre = "1340", + code_region = "52", + date_debut_prelevement = "2021-01-01", + date_fin_prelevement = "2021-12-31", + size = "5000") +``` + +```{r create-nitrate_analyse_esu_2022, eval=FALSE} +nitrate_analyse_esu_2022 <- get_json_data(code_parametre = "1340", + code_region = "52", + date_debut_prelevement = "2022-01-01", + date_fin_prelevement = "2022-12-31", + size = "5000") +``` + +```{r development-inflate, eval=FALSE} +# Keep eval=FALSE to avoid infinite loop in case you hit the knit button +# Execute in the console directly +fusen::inflate(flat_file = "dev/flat_import_data.Rmd", vignette_name = "Import des données") +``` + + +# Inflate your package + +You're one inflate from paper to box. +Build your package from this very Rmd 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 diff --git a/vignettes/import-des-donnees.Rmd b/vignettes/import-des-donnees.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..803a56a578ce8c9de978ccf4f197a970beb5f419 --- /dev/null +++ b/vignettes/import-des-donnees.Rmd @@ -0,0 +1,121 @@ +--- +title: "Import des données" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{import-des-donnees} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +```{r setup} +library(data.nitrates) +``` + +<!-- WARNING - This vignette is generated by {fusen} from dev/flat_import_data.Rmd: do not edit by hand --> + +# Présentation + +> Cette page contient la logique métier concernant l'import des données +ARS et Hub'eau dans le cadre du projet Nitrates + + +# Connexion à la base de données PostgreSQL + +# Import des données de l'ARS + +```{r} +data_ars_2022 <- collectr::import_xlsx( + filepath = "T:\\datalab\\SRNP_DEMA_SI_EAU\\NITRATES\\DONNEES_CLIENT\\SOURCES\\ARS\\2023_06\\Nitrates 2022 pour DREAL EPNT4 2023-05-30.xlsx", + sheet = 1, + row = 2) +``` + +## Première approche : non concluante + +### Vérification de la correspondande de la structure du dataframe avec celle de la table + +### Adaptation de la structure + +### Insertion des données du dataframe dans la table en base + +La fonction insert_into_table() doit être corrigée. + + +## Deuxième approche : concluante + +### Création d'une table pour le nouveau millésime + +```{r create-nitrate_data_ars, eval = FALSE} +poster_data(data = data_ars_2022, + table = "nitrate_data_ars_2022", + schema = "qualite_cours_d_eau", + db = "si_eau", + overwrite = TRUE) + +``` + +### Modification du type des champs si besoin + +```{r modify_column_type, eval = FALSE} +# Appeler la fonction pour modifier le type de colonne +collectr::modify_column_type(connexion, + schema = "qualite_cours_d_eau", + table_name = "nitrate_data_ars_2022", + column_name = "plv_date ", + column_type = "date") +``` + +### Insertion des données du nouveau millésime en base dans la table globale + +```{r import_and_merge_tables, eval = FALSE} +import_and_merge_tables(connexion, + source_table = "nitrate_data_ars_2022", + source_schema = "qualite_cours_d_eau", + target_table = "nitrate_data_analyse_ars_test", + target_schema = "qualite_cours_d_eau") + +``` + +# Import des données Hub'eau ESU + +```{r create-nitrate_analyse_esu_2020, eval = FALSE} +nitrate_analyse_esu_2020 <- get_json_data(code_parametre = "1340", + code_region = "52", + date_debut_prelevement = "2020-01-01", + date_fin_prelevement = "2020-12-31", + size = "5000") +``` + +```{r create-nitrate_analyse_esu_2021, eval = FALSE} +nitrate_analyse_esu_2021 <- get_json_data(code_parametre = "1340", + code_region = "52", + date_debut_prelevement = "2021-01-01", + date_fin_prelevement = "2021-12-31", + size = "5000") +``` + +```{r create-nitrate_analyse_esu_2022, eval = FALSE} +nitrate_analyse_esu_2022 <- get_json_data(code_parametre = "1340", + code_region = "52", + date_debut_prelevement = "2022-01-01", + date_fin_prelevement = "2022-12-31", + size = "5000") +``` + +# Inflate your package + +You're one inflate from paper to box. +Build your package from this very Rmd 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 +