Skip to content

Commit

Permalink
major update to vers 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcarayon committed Feb 3, 2020
1 parent ae2f4ca commit 58736c9
Show file tree
Hide file tree
Showing 22 changed files with 981 additions and 361 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.Rproj.user
.Rhistory
.RData
inst/doc
11 changes: 7 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: IDGF
Title: Indice Diatomique de Guyane Française
Version: 1.0.000
Version: 2.0
Authors@R: c(
person("David", "Carayon", email = "david.carayon@irstea.fr", role = c("aut", "cre")),
person("François","Delmas",email = "francois.delmas@irstea.fr", role = "aut")
person("David", "Carayon", email = "david.carayon@inrae.fr", role = c("aut", "cre")),
person("François","Delmas",email = "francois.delmas@inrae.fr", role = "aut")
)
Description: Package contenant les fonctions essentielles pour l'évaluation de l'état écologique des cours d'eau de Guyane basée sur les diatomées.
Depends: R (>= 3.4.0)
Expand All @@ -16,5 +16,8 @@ Imports:
dplyr,
tidyr,
ggplot2,
crayon,
purrr
Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
7 changes: 4 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(Diagnostic_IDGF)
export(IDGF)
export(computeIDGF)
export(exportIDGF)
export(importIDGF)
export(radarIDGF)
importFrom(magrittr,"%>%")
importFrom(stats,na.omit)
156 changes: 0 additions & 156 deletions R/IDGF.R

This file was deleted.

77 changes: 77 additions & 0 deletions R/computeIDGF.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#' Calcul de l'IDGF
#'
#' @param IDGFdata Données importées par `importIDGF()`
#'
#' @return une liste
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
computeIDGF <- function(IDGFdata){

df_standard <- IDGFdata$dataset


metrique_calc <- df_standard %>%
dplyr::inner_join(table_metrics, by = "cd_taxon") %>%
dplyr::mutate_at(.vars = dplyr::vars(indiciel:NH4),.funs = dplyr::funs(.*abondance)) %>%
dplyr::group_by(sample) %>%
dplyr::summarise_at(dplyr::vars(indiciel:NH4),sum) %>%
dplyr::mutate_at(.vars = dplyr::vars(MINER:NH4),.funs=dplyr::funs(1-./indiciel)) %>%
dplyr::ungroup() %>%
dplyr::rowwise() %>%
dplyr:: mutate(MORGA = min(COT,DBO5,DCO),
PTROPHIE = min(PO4,PTOT),
NORG = min(NK,NH4)) %>%
dplyr::ungroup() %>%
dplyr::select(-COT:-DCO,-NK,-NH4,-PO4,-PTOT) %>%
tidyr::gather(key = param, value = value, -sample:-indiciel,-halin) %>%
dplyr::inner_join(IDGFdata$metadata, by = "sample")


tab_indiciel <- metrique_calc %>%
dplyr::select(sample,indiciel,halin) %>%
unique() %>%
dplyr::mutate(`fiabilité` = dplyr::case_when(indiciel < 300 ~ "Indice non valide, augmenter la pression de comptage",
indiciel >= 300 & indiciel < 360 ~ "Fiabilité : réduite",
indiciel >= 360 ~ "Fiabilité : satisfaisante"))


metriques <- metrique_calc %>%
dplyr::group_by(sample,her,param) %>%
dplyr::mutate(EQR = purrr::pmap_dbl(.l = list(value,her,param), .f = Metric2EQR)) %>% # Renvoie metrique/ref
dplyr::ungroup() %>%
dplyr::select(sample,param,EQR) %>%
tidyr::spread(key = param, value = EQR) %>%
dplyr::mutate_at(dplyr::vars(MES:SAT),round,2)


note_finale <- metrique_calc %>%
dplyr::group_by(sample,her,param) %>%
dplyr::mutate(EQR = purrr::pmap_dbl(.l = list(value,her,param), .f = Metric2EQR)) %>% # Renvoie metrique/ref
dplyr::ungroup() %>%
dplyr::group_by(sample) %>%
dplyr::summarise(IDGF = mean(EQR)) %>% # Note IDGF = moyenne des EQR
dplyr::ungroup() %>%
dplyr::mutate(IDGF = model_IDGF(IDGF)) %>%
dplyr::mutate(IDGF = round(IDGF,2)) %>%
dplyr::mutate(class = Ratio2Class(IDGF,boundaries = c(0.25,0.50,0.75,0.88),number = FALSE,language = "FR"),
numclass = Ratio2Class(IDGF,boundaries = c(0.25,0.50,0.75,0.88),number = TRUE,language = "FR")) %>%
dplyr::inner_join(metriques, by = "sample") %>%
dplyr::inner_join(tab_indiciel, by = "sample") %>%
dplyr::mutate(indiciel = ifelse(indiciel > 400, yes = 400, no = indiciel)) %>%
dplyr::mutate(halin = round(halin / 400 * 100,2),
indiciel = round(indiciel / 400 * 100,2)) %>%

dplyr::left_join(IDGFdata$taxons_missing, by = "sample") %>%
dplyr::left_join(IDGFdata$taxons_halins, by = "sample") %>%
dplyr::mutate_at(dplyr::vars(IDGF,class:SAT,Taxons_inconnus,Taxons_halins),
dplyr::funs(ifelse(is.na(.), "", .))) %>%
dplyr::select(id_releve = sample,pourcentage_indiciels = indiciel,pourcentage_halins = halin, MES:SAT,IDGF,NumClasse = numclass, Classe = class, `fiabilité`, Taxons_halins,Taxons_inconnus) %>%
dplyr::rename("MINE."="MINER","SAT.O2"="SAT","Mat.Orga"="MORGA","P-Trophie"="PTROPHIE","N-Orga"="NORG")


return(note_finale)


}
44 changes: 44 additions & 0 deletions R/exportIDGF.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Export des résultats et graphiques issus de l'IDGF
#'
#' @param IDGFres résultats IDGF issus de `computeIDGF()` ou `radarIDGF()`
#' @param outdir Dossier de sortie à définir pour les résultats
#'
#' @return
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
exportIDGF <- function(IDGFres, outdir = paste0("RES_",Sys.Date())){


if(!dir.exists(outdir)) {dir.create(outdir, recursive = TRUE)}

s.time <- paste0(strsplit(as.character(Sys.time())," ")[[1]],collapse = "_")
time <- paste0(strsplit(s.time,":")[[1]],collapse = "-")

write.csv2(IDGFres %>% dplyr::select(1:16),paste0(outdir,"/resultats_idgf_",time,".csv"), row.names = FALSE)

## Sortie des graphiques si ils sont présent dans le tableau de résultat
if(ncol(IDGFres) == 17) {

output <- file.path(outdir,"Graphiques_diagnostic")

if(!dir.exists(output)) {dir.create(output, recursive = TRUE)}

tab_res <- IDGFres %>% dplyr::select(id_releve,plot) %>%
dplyr::mutate(path = file.path(output,id_releve)) %>%
dplyr::mutate(png_path = glue::glue("{path}.png"))


export_plot <- function(id_releve,plot,png_path) {

print(plot) %>%
ggplot2::ggsave(filename=png_path,dpi = "retina", width = 7.62, height = 6.48)
}

purrr::pwalk(.l = list(tab_res$id_releve,tab_res$plot,tab_res$png_path), .f = export_plot)


}

}
77 changes: 77 additions & 0 deletions R/importIDGF.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#' Import des données
#'
#' @param input Le nom du fichier de données à importer (ou un chemin)
#'
#' @return une liste
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
importIDGF <- function(input){

filetype <- tools::file_ext(input)

if(filetype %in% c("xls","xlsx")) {

df <- suppressWarnings(readxl::read_excel(input))

} else if (filetype == "csv") {

df <- suppressWarnings(readr::read_csv2(input) %>% dplyr::select(-X1))

} else {stop("Merci d'utiliser un fichier d'entrée au format .xls, .xlsx ou .csv")}


# Test unitaires

if(ncol(df) != 4) {
stop('l\'argument df a besoin d\'un tableau avec 4 colonnes précisément : id_releve, cd_taxon, abondance, her (1 ou 2)')
}

# Standardisation (transcodage + abondance)

names(df) <- c("sample","cd_taxon","abondance","her")
if(is.factor(df$cd_taxon)) {df$cd_taxon = as.character(df$cd_taxon)}

tab_her <- df %>% dplyr::select(sample,her) %>% dplyr::distinct()

taxons_missing<- subset(df, !(cd_taxon %in% table_metrics$cd_taxon)) %>%
dplyr::group_by(sample) %>%
dplyr::summarise(Taxons_inconnus = paste(cd_taxon,collapse = ","))

taxons_halins <- subset(df, cd_taxon %in% subset(table_metrics,halin == 1)$cd_taxon) %>%
dplyr::group_by(sample) %>%
dplyr::summarise(Taxons_halins = paste(cd_taxon,collapse = ","))


# Ajout des taxons manquants
if(nrow(taxons_missing>0)) {

newlines <- suppressWarnings(taxons_missing %>%
dplyr::select(cd_taxon = Taxons_inconnus) %>%
dplyr::bind_rows(table_metrics) %>%
dplyr::mutate_at(dplyr::vars(indiciel:NH4),
dplyr::funs(ifelse(is.na(.), 0, .))))
}


df_standard <- df %>%
dplyr::left_join(transcode, by = c("cd_taxon"="code")) %>%
dplyr::select(-cd_taxon) %>%
dplyr::group_by(sample,IDGF_v1) %>%
dplyr::summarise(abondance = sum(abondance, na.rm=TRUE)) %>%
dplyr::ungroup() %>%
dplyr::group_by(sample) %>%
dplyr::mutate(n_valves = sum(abondance)) %>%
dplyr::ungroup() %>%
dplyr::rowwise() %>%
dplyr::mutate(abondance = round(abondance / n_valves * 400)) %>%
dplyr::ungroup() %>%
dplyr::select(sample,cd_taxon = IDGF_v1,abondance)


res <- list(dataset = df_standard, metadata = tab_her, taxons_missing = taxons_missing, taxons_halins = taxons_halins)

return(res)

}
Loading

0 comments on commit 58736c9

Please sign in to comment.