results_manager.R 2.57 KiB
# \\\
# 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.
write_listofdf = function (Ldf, resdir, filedir) {
    outdir = file.path(resdir, filedir)
    if (!(file.exists(outdir))) {
        dir.create(outdir)
    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)
# write_listofdf(res_tINItrend, resdir, 'res_tINItrend')
read_listofdf = function (resdir, filedir) {
    outdir = file.path(resdir, filedir)
    files = list.files(outdir)
    Nfile = length(files)
    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 {
7172737475767778798081828384858687888990
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) } # res_tINItrend = read_listofdf(resdir, 'res_tINItrend') splitext = function(file) { ex = strsplit(basename(file), split="\\.")[[1]] res = list(name=ex[1], extension=ex[2]) return (res) }