Commit f8201710 authored by Dorchies David's avatar Dorchies David
Browse files

Merge branch '16-add-a-tidy_result-function' into 'main'

Resolve "Add a `tidy_result` function"

Closes #17 and #16

See merge request !12
1 merge request!12Resolve "Add a `tidy_result` function"
Pipeline #34289 passed with stage
in 2 minutes and 41 seconds
Showing with 75 additions and 2 deletions
+75 -2
......@@ -25,4 +25,5 @@ Imports:
magrittr,
terra,
tidyquery,
tidyr,
xml2
......@@ -30,6 +30,7 @@ export(sic_run_steady)
export(sic_run_unsteady)
export(sic_write_par)
export(split_reach)
export(tidy_result)
export(update_portion_abscissas)
import(magrittr)
import(utils)
......
......@@ -2,6 +2,7 @@
#'
#' @inheritParams sic_run_mesh
#' @param filters [character] conditions to select columns in result table, see details
#' @param tidy [logical], if TRUE the result is returned after being processed by [tidy_result]
#' @param m [matrix] of results produced by [read_bin_result_matrix]
#'
#' @return [matrix] of results with a first column "t" with the simulation time
......@@ -26,6 +27,7 @@ get_result <- function(cfg,
scenario,
variant = 0,
filters = c(""),
tidy = TRUE,
m = read_bin_result_matrix(cfg, scenario, variant)) {
df_col <- get_result_tree(cfg, scenario, variant)
......@@ -47,6 +49,7 @@ get_result <- function(cfg,
time_prms <- sapply(attrs, function(attr) {
as.numeric(xml_attr(x_res, attr))
})
tms <- seq(from = time_prms["TpsDebut"],
to = time_prms["TpsFin"],
by = time_prms["TpsPas"] * time_prms["TpsSauv"])
......@@ -67,9 +70,13 @@ get_result <- function(cfg,
paste(cols, collapse = "|")
})
m <- cbind(tms, m)
m <- cbind(tms, m[1:length(tms), ])
colnames(m) <- c("t", column_names)
class(m) <- c("SicResult", class(m))
if (tidy) {
return(tidy_result(m))
}
return(m)
}
......
#' Tidy a result simulation
#'
#' @param res a *SicResult* [matrix] provided by [get_result]
#'
#' @return A [data.frame] with one line per saved simulation result time step :
#'
#' - one column per object type of the result (example: "bf" and "sn" for a section or "nd" and "pr" for an offtake)
#' - one column "var" for the definition of the result variable
#' - one column "t" for the simulation time of the result variable
#' - one columne "value" for the value of the result variable
#'
#' @export
#'
#' @examples
tidy_result <- function(res) {
stopifnot(inherits(res, "SicResult"))
res <- as.data.frame(res)
df <- tidyr::gather(res, key = "key", value = "value", -"t")
keys <- strsplit(df$key, "|", fixed = TRUE)
l <- lapply(keys, function(x) {
l <- lapply(strsplit(x, ":", fixed = TRUE),
function(obj) {
df <- data.frame(x = obj[2])
names(df) <- obj[1]
return(df)
})
do.call(cbind, l)
})
df_obj <- do.call(rbind, l)
df$key <- NULL
cbind(df_obj, df)
}
......@@ -9,6 +9,7 @@ get_result(
scenario,
variant = 0,
filters = c(""),
tidy = TRUE,
m = read_bin_result_matrix(cfg, scenario, variant)
)
}
......@@ -21,6 +22,8 @@ get_result(
\item{filters}{\link{character} conditions to select columns in result table, see details}
\item{tidy}{\link{logical}, if TRUE the result is returned after being processed by \link{tidy_result}}
\item{m}{\link{matrix} of results produced by \link{read_bin_result_matrix}}
}
\value{
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tidy_result.R
\name{tidy_result}
\alias{tidy_result}
\title{Tidy a result simulation}
\usage{
tidy_result(res)
}
\arguments{
\item{res}{a \emph{SicResult} \link{matrix} provided by \link{get_result}}
}
\value{
A \link{data.frame} with one line per saved simulation result time step :
\itemize{
\item one column per object type of the result (example: "bf" and "sn" for a section or "nd" and "pr" for an offtake)
\item one column "var" for the definition of the result variable
\item one column "t" for the simulation time of the result variable
\item one columne "value" for the value of the result variable
}
}
\description{
Tidy a result simulation
}
......@@ -4,8 +4,14 @@ cfg <- cfg_tmp_project()
sic_run_steady(cfg, scenario = 1)
test_that("get_result returns a matrix with correct colnames", {
result <- get_result(cfg, 1, filters = c("bf=4", "var='Z'"))
result <- get_result(cfg, 1, filters = c("bf=4", "var='Z'"), tidy = FALSE)
expect_true(is.matrix(result))
expect_type(result, "double")
expect_equal(colnames(result), c("t", sprintf("bf:4|sn:%d|var:Z", 1:4)))
})
test_that("get_result with tidy return a tidy result", {
result <- get_result(cfg, 1, filters = c("bf=4", "var='Z'"))
expect_s3_class(result, "data.frame")
expect_equal(names(result), c("bf", "sn", "var", "t", "value"))
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment