-
Heraut Louis authoredb7a16b14
# \\\
# Copyright 2021-2022 Louis Héraut*1,
# Éric Sauquet*2,
# Valentin Mansanarez
#
# *1 INRAE, France
# louis.heraut@inrae.fr
# *2 INRAE, France
# eric.sauquet@inrae.fr
#
# This file is part of ash R toolbox.
#
# Ash R toolbox is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Ash R toolbox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ash R toolbox.
# If not, see <https://www.gnu.org/licenses/>.
# ///
#
#
# Rcode/processing/results_manager.R
#
# Manages the writing and reading of results data of the trend analysis.
## 1. WRITING ________________________________________________________
### 1.1. List of dataframe ___________________________________________
write_analyse = function (Ldf, resdir, filedir, optdir='') {
outdir = file.path(resdir, optdir, filedir)
if (!(file.exists(outdir))) {
dir.create(outdir, recursive=TRUE)
}
print(paste('Writing of list of dataframe in : ', outdir, sep=''))
Lname = names(Ldf)
Nname = length(Lname)
for (i in 1:Nname) {
outfile = paste(Lname[i], '.txt', sep='')
write.table(Ldf[[i]],
file=file.path(outdir, outfile),
sep=";",
quote=TRUE,
row.names=FALSE)
}
}
### 2.2. Dataframe of modified data __________________________________
write_data = function (df_data, df_mod, resdir, filedir, optdir='',
mod_file=TRUE) {
Code = rle(sort(df_mod$code))$values
outdir = file.path(resdir, optdir, filedir)
if (!(file.exists(outdir))) {
dir.create(outdir, recursive=TRUE)
}
print(paste('Writing of modified data in : ', outdir, sep=''))
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
for (code in Code) {
df_data_code = df_data[df_data$code == code,]
df_mod_code = df_mod[df_mod$code == code,]
outfile1 = paste(code, '.txt', sep='')
write.table(df_data_code,
file=file.path(outdir, outfile1),
sep=";",
quote=TRUE,
row.names=FALSE)
if (mod_file) {
outfile2 = paste(code, '_modification', '.txt', sep='')
write.table(df_mod_code,
file=file.path(outdir, outfile2),
sep=";",
quote=TRUE,
row.names=FALSE)
}
}
}
### 2.3. Dataframe of criticism ______________________________________
write_critique = function (df_critique, resdir, filename='critique', optdir='') {
outdir = file.path(resdir, optdir)
if (!(file.exists(outdir))) {
dir.create(outdir, recursive=TRUE)
}
print(paste('Writing criticism in : ', outdir, sep=''))
outfile = paste(filename, '.txt', sep='')
write.table(df_critique,
file=file.path(outdir, outfile),
sep=";",
quote=FALSE,
row.names=FALSE)
}
# write_critique(df_critique, resdir)
## 2. READING ________________________________________________________
### 2.1. List of dataframe ___________________________________________
read_listofdf = function (resdir, filedir) {
outdir = file.path(resdir, filedir)
files = list.files(outdir)
Nfile = length(files)
print(paste('Reading of list of dataframe in : ', outdir, sep=''))
Ldf = list()
for (i in 1:Nfile) {
name = splitext(files[i])$name
df = as_tibble(read.table(file=file.path(outdir, files[i]),
header=TRUE,
sep=";",
quote='"'))
for (j in 1:ncol(df)) {
if (is.factor(df[[j]])) {
d = try(as.Date(df[[1, j]], format="%Y-%m-%d"))
if("try-error" %in% class(d) || is.na(d)) {
df[j] = as.character(df[[j]])
} else {
df[j] = as.Date(df[[j]])
}
}
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
}
# OkFact = sapply(df, is.factor)
# df[OkFact] = lapply(df[OkFact], as.character)
Ldf = append(Ldf, list(df))
names(Ldf)[length(Ldf)] = name
}
return (Ldf)
}
### 2.3. Dataframe of criticism ______________________________________
read_critique = function (resdir, filename='critique', optdir='') {
outdir = file.path(resdir, optdir)
outfile = paste(filename, '.txt', sep='')
file_path = file.path(outdir, outfile)
print(paste('Reading criticism in : ', file_path, sep=''))
df = as_tibble(read.table(file=file_path,
header=TRUE,
sep=";"))
for (j in 1:ncol(df)) {
if (is.factor(df[[j]])) {
d = try(as.Date(df[[1, j]], format="%Y-%m-%d"))
if("try-error" %in% class(d) || is.na(d)) {
df[j] = as.character(df[[j]])
} else {
df[j] = as.Date(df[[j]])
}
}
}
return (df)
}
# df_critique = read_critique(resdir)
## 3. OTHER __________________________________________________________
splitext = function(file) {
ex = strsplit(basename(file), split="\\.")[[1]]
res = list(name=ex[1], extension=ex[2])
return (res)
}