diff --git a/NAMESPACE b/NAMESPACE index ff4f19a..686757b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -71,6 +71,8 @@ export(get_run_param_csv) export(get_run_state) export(get_run_status) export(get_run_table) +export(get_run_table_calc_csv) +export(get_run_table_comparison_csv) export(get_run_table_csv) export(get_runs) export(get_scenarios) diff --git a/R/OutputTables.R b/R/OutputTables.R index a04d9a8..d31655f 100644 --- a/R/OutputTables.R +++ b/R/OutputTables.R @@ -6,6 +6,13 @@ #' @param run Model run digest, run stamp or run name, modeling task run #' stamp or task run name. #' @param name Output table name. +#' @param calc Name of calculation. One of `"avg"`, `"sum"`, `"count"`, `"max"`, +#' `"min"`, `"var"`, `"sd"`, `"se"`, or `"cv"`. +#' @param compare Comparison to calculate. One of `"diff"`, `"ratio"`, or +#' `"percent"`. Comparisons are for the base run relative to the variant +#' run (i.e., for `"diff"` it is the difference of values represented as +#' Variant - Base). +#' @param variant Run digest, name, or stamp for the variant model run. #' #' @return A `list` or `tibble`. #' @@ -29,4 +36,31 @@ get_run_table_csv <- function(model, run, name) { readr::read_csv(show_col_types = FALSE, progress = FALSE) } +#' @rdname get_run_table +#' @export +get_run_table_calc_csv <- function(model, run, name, calc) { + rlang::arg_match(calc, c('avg', 'sum', 'count', 'max', 'min', 'var', + 'sd', 'se', 'cv')) + + api_path <- glue::glue('api/model/{model}/run/{run}/table/{name}/calc/{calc}/csv') + OpenMpp$API$build_request() |> + httr2::req_url_path(api_path) |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE, progress = FALSE) +} + +#' @rdname get_run_table +#' @export +get_run_table_comparison_csv <- function(model, run, name, compare, variant) { + rlang::arg_match(compare, c('diff', 'ratio', 'percent')) + + api_path <- glue::glue('api/model/{model}/run/{run}/table/{name}/compare/{compare}/variant/{variant}/csv') + OpenMpp$API$build_request() |> + httr2::req_url_path(api_path) |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE, progress = FALSE) +} + utils::globalVariables(c('data')) diff --git a/R/R6ClassModelRun.R b/R/R6ClassModelRun.R index f3d4d81..a52cdf3 100644 --- a/R/R6ClassModelRun.R +++ b/R/R6ClassModelRun.R @@ -93,6 +93,39 @@ OpenMppModelRun <- rlang::abort(abort_msg) }, + #' @description + #' Retrieve a table calculation. + #' @param name Table name. + #' @param calc Name of calculation. One of `"avg"`, `"sum"`, `"count"`, + #' `"max"`, `"min"`, `"var"`, `"sd"`, `"se"`, or `"cv"`. + #' @return A `tibble`. + get_table_calc = function(name, calc) { + if (name %in% private$.tables) { + tbl <- get_run_table_calc_csv(self$ModelDigest, self$RunDigest, name, calc) + suppressWarnings(tbl$calc_value <- as.numeric(tbl$calc_value)) + return(tbl) + } + abort_msg <- glue::glue('Table `{name}` is not available for model run.') + rlang::abort(abort_msg) + }, + + #' @description + #' Retrieve a table comparison. + #' @param name Table name. + #' @param compare Comparison to calculate. One of `"diff"`, `"ratio"`, or + #' `"percent"`. + #' @param variant Run digest, name, or stamp for the variant model run. + #' @return A `tibble`. + get_table_comparison = function(name, compare, variant) { + if (name %in% private$.tables) { + tbl <- get_run_table_comparison_csv(self$ModelDigest, self$RunDigest, name, compare, variant) + suppressWarnings(tbl$calc_value <- as.numeric(tbl$calc_value)) + return(tbl) + } + abort_msg <- glue::glue('Table `{name}` is not available for model run.') + rlang::abort(abort_msg) + }, + #' @description #' Write an output table to disk (CSV). #' @param name Table name. diff --git a/R/R6ClassModelRunSet.R b/R/R6ClassModelRunSet.R index ff9a80f..0ede5be 100644 --- a/R/R6ClassModelRunSet.R +++ b/R/R6ClassModelRunSet.R @@ -83,6 +83,17 @@ OpenMppModelRunSet <- purrr::list_rbind(names_to = 'RunName') }, + #' @description + #' Retrieve a table calculation. + #' @param name Table name. + #' @param calc Name of calculation. One of `"avg"`, `"sum"`, `"count"`, + #' `"max"`, `"min"`, `"var"`, `"sd"`, `"se"`, or `"cv"`. + #' @return A `tibble`. + get_table_calc = function(name, calc) { + purrr::map(private$.runs, \(run) run$get_table_calc(name, calc)) |> + purrr::list_rbind(names_to = 'RunName') + }, + #' @description #' Write an output table to disk (CSV). #' @param name Table name. diff --git a/man/get_run_table.Rd b/man/get_run_table.Rd index 90cef8b..a14ea8b 100644 --- a/man/get_run_table.Rd +++ b/man/get_run_table.Rd @@ -3,11 +3,17 @@ \name{get_run_table} \alias{get_run_table} \alias{get_run_table_csv} +\alias{get_run_table_calc_csv} +\alias{get_run_table_comparison_csv} \title{OpenM++ Output Tables} \usage{ get_run_table(model, run, name) get_run_table_csv(model, run, name) + +get_run_table_calc_csv(model, run, name, calc) + +get_run_table_comparison_csv(model, run, name, compare, variant) } \arguments{ \item{model}{Model digest or model name.} @@ -16,6 +22,16 @@ get_run_table_csv(model, run, name) stamp or task run name.} \item{name}{Output table name.} + +\item{calc}{Name of calculation. One of \code{"avg"}, \code{"sum"}, \code{"count"}, \code{"max"}, +\code{"min"}, \code{"var"}, \code{"sd"}, \code{"se"}, or \code{"cv"}.} + +\item{compare}{Comparison to calculate. One of \code{"diff"}, \code{"ratio"}, or +\code{"percent"}. Comparisons are for the base run relative to the variant +run (i.e., for \code{"diff"} it is the difference of values represented as +Variant - Base).} + +\item{variant}{Run digest, name, or stamp for the variant model run.} } \value{ A \code{list} or \code{tibble}. diff --git a/man/load_model_run.Rd b/man/load_model_run.Rd index c76cd19..0e2588f 100644 --- a/man/load_model_run.Rd +++ b/man/load_model_run.Rd @@ -62,6 +62,8 @@ OpenM++ ModelRun Class \item \href{#method-OpenMppModelRun-new}{\code{OpenMppModelRun$new()}} \item \href{#method-OpenMppModelRun-print}{\code{OpenMppModelRun$print()}} \item \href{#method-OpenMppModelRun-get_table}{\code{OpenMppModelRun$get_table()}} +\item \href{#method-OpenMppModelRun-get_table_calc}{\code{OpenMppModelRun$get_table_calc()}} +\item \href{#method-OpenMppModelRun-get_table_comparison}{\code{OpenMppModelRun$get_table_comparison()}} \item \href{#method-OpenMppModelRun-write_table}{\code{OpenMppModelRun$write_table()}} \item \href{#method-OpenMppModelRun-write_tables}{\code{OpenMppModelRun$write_tables()}} \item \href{#method-OpenMppModelRun-get_log}{\code{OpenMppModelRun$get_log()}} @@ -131,6 +133,54 @@ A \code{tibble}. } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-OpenMppModelRun-get_table_calc}{}}} +\subsection{Method \code{get_table_calc()}}{ +Retrieve a table calculation. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{OpenMppModelRun$get_table_calc(name, calc)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{name}}{Table name.} + +\item{\code{calc}}{Name of calculation. One of \code{"avg"}, \code{"sum"}, \code{"count"}, +\code{"max"}, \code{"min"}, \code{"var"}, \code{"sd"}, \code{"se"}, or \code{"cv"}.} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +A \code{tibble}. +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-OpenMppModelRun-get_table_comparison}{}}} +\subsection{Method \code{get_table_comparison()}}{ +Retrieve a table comparison. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{OpenMppModelRun$get_table_comparison(name, compare, variant)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{name}}{Table name.} + +\item{\code{compare}}{Comparison to calculate. One of \code{"diff"}, \code{"ratio"}, or +\code{"percent"}.} + +\item{\code{variant}}{Run digest, name, or stamp for the variant model run.} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +A \code{tibble}. +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-OpenMppModelRun-write_table}{}}} \subsection{Method \code{write_table()}}{ diff --git a/man/load_model_runs.Rd b/man/load_model_runs.Rd index 5a4ab70..c6cae82 100644 --- a/man/load_model_runs.Rd +++ b/man/load_model_runs.Rd @@ -60,6 +60,7 @@ OpenM++ ModelRunSet Class \item \href{#method-OpenMppModelRunSet-new}{\code{OpenMppModelRunSet$new()}} \item \href{#method-OpenMppModelRunSet-print}{\code{OpenMppModelRunSet$print()}} \item \href{#method-OpenMppModelRunSet-get_table}{\code{OpenMppModelRunSet$get_table()}} +\item \href{#method-OpenMppModelRunSet-get_table_calc}{\code{OpenMppModelRunSet$get_table_calc()}} \item \href{#method-OpenMppModelRunSet-write_table}{\code{OpenMppModelRunSet$write_table()}} \item \href{#method-OpenMppModelRunSet-write_tables}{\code{OpenMppModelRunSet$write_tables()}} } @@ -127,6 +128,29 @@ A \code{tibble}. } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-OpenMppModelRunSet-get_table_calc}{}}} +\subsection{Method \code{get_table_calc()}}{ +Retrieve a table calculation. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{OpenMppModelRunSet$get_table_calc(name, calc)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{name}}{Table name.} + +\item{\code{calc}}{Name of calculation. One of \code{"avg"}, \code{"sum"}, \code{"count"}, +\code{"max"}, \code{"min"}, \code{"var"}, \code{"sd"}, \code{"se"}, or \code{"cv"}.} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +A \code{tibble}. +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-OpenMppModelRunSet-write_table}{}}} \subsection{Method \code{write_table()}}{