-
Heraut Louis authoredf4ff6d05
# \\\
# Copyright 2021-2022 Louis Héraut*1
#
# *1 INRAE, France
# louis.heraut@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/>.
# ///
#
#
# results_manager.R
#
# Manages the writing and reading of results data of the trend analysis.
## 1. WRITING ________________________________________________________
### 1.1. List of dataframe ___________________________________________
write_listofdf = 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_dfdata = function (df_data, df_mod, resdir, filedir, optdir='') {
Code = rle(sort(df_mod$code))$values
print(Code)
outdir = file.path(resdir, optdir, filedir)
if (!(file.exists(outdir))) {
dir.create(outdir, recursive=TRUE)
}
print(paste('Writing of modified data in : ', outdir, sep=''))
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,
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
file=file.path(outdir, outfile1),
sep=";",
quote=TRUE,
row.names=FALSE)
outfile2 = paste(code, '_modification', '.txt', sep='')
write.table(df_mod_code,
file=file.path(outdir, outfile2),
sep=";",
quote=TRUE,
row.names=FALSE)
}
}
## 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]])
}
}
}
# OkFact = sapply(df, is.factor)
# df[OkFact] = lapply(df[OkFact], as.character)
Ldf = append(Ldf, list(df))
names(Ldf)[length(Ldf)] = name
}
return (Ldf)
}
## 3. OTHER __________________________________________________________
splitext = function(file) {
ex = strsplit(basename(file), split="\\.")[[1]]
res = list(name=ex[1], extension=ex[2])
return (res)
}