From 72ea59b617809396f1174f7b96877c9f00a453ae Mon Sep 17 00:00:00 2001 From: Lukas Gessl Date: Mon, 11 Dec 2023 16:02:33 +0000 Subject: [PATCH] write and test prepend_to_save_dir() --- NAMESPACE | 1 + R/data_spec.R | 22 ++++++++++++++++++++++ man/prepend_to_save_dir.Rd | 23 +++++++++++++++++++++++ tests/testthat/test-data_spec.R | 10 ++++++++++ 4 files changed, 56 insertions(+) create mode 100644 man/prepend_to_save_dir.Rd create mode 100644 tests/testthat/test-data_spec.R diff --git a/NAMESPACE b/NAMESPACE index f54667e..879a585 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,7 @@ export(mock_data_from_existing) export(prepare) export(prepare_and_fit) export(prepare_and_predict) +export(prepend_to_save_dir) export(qc_prepare) export(qc_preprocess) export(read) diff --git a/R/data_spec.R b/R/data_spec.R index daeb44c..4aeabc8 100644 --- a/R/data_spec.R +++ b/R/data_spec.R @@ -86,4 +86,26 @@ DataSpec <- function( gene_id_col = gene_id_col ) return(data_spec) +} + + +#' @title To every `ModelSpec` in a list of `ModelSpec`s, prepend a fixed directory to the +#' `save_dir` attribute +#' @description Often one specifies the models in general, for all data sets. If you fit the +#' models to a specific data set (say `"mock"`), you might want to prepend a fixed directory +#' like `"results/mock"` to the `save_dir` attribute of all `ModelSpec`s in the list. +#' @param model_spec_list list of `ModelSpec`s. +#' @param dir string. The directory to prepend to the `save_dir` attribute of all +#' `ModelSpec`s in `model_spec_list`. +#' @return A list of `ModelSpec`s, with `dir` prepended to the `save_dir` attribute. +#' @export +prepend_to_save_dir <- function( + model_spec_list, + dir +){ + stopifnot(is.character(dir)) + for(i in seq_along(model_spec_list)){ + model_spec_list[[i]]$save_dir <- file.path(dir, model_spec_list[[i]]$save_dir) + } + return(model_spec_list) } \ No newline at end of file diff --git a/man/prepend_to_save_dir.Rd b/man/prepend_to_save_dir.Rd new file mode 100644 index 0000000..a771409 --- /dev/null +++ b/man/prepend_to_save_dir.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_spec.R +\name{prepend_to_save_dir} +\alias{prepend_to_save_dir} +\title{To every \code{ModelSpec} in a list of \code{ModelSpec}s, prepend a fixed directory to the +\code{save_dir} attribute} +\usage{ +prepend_to_save_dir(model_spec_list, dir) +} +\arguments{ +\item{model_spec_list}{list of \code{ModelSpec}s.} + +\item{dir}{string. The directory to prepend to the \code{save_dir} attribute of all +\code{ModelSpec}s in \code{model_spec_list}.} +} +\value{ +A list of \code{ModelSpec}s, with \code{dir} prepended to the \code{save_dir} attribute. +} +\description{ +Often one specifies the models in general, for all data sets. If you fit the +models to a specific data set (say \code{"mock"}), you might want to prepend a fixed directory +like \code{"results/mock"} to the \code{save_dir} attribute of all \code{ModelSpec}s in the list. +} diff --git a/tests/testthat/test-data_spec.R b/tests/testthat/test-data_spec.R new file mode 100644 index 0000000..aafdf2f --- /dev/null +++ b/tests/testthat/test-data_spec.R @@ -0,0 +1,10 @@ +test_that("prepend_to_save_dir() works", { + + model_spec_list = list( + ModelSpec("mock1", fitter = zeroSum::zeroSum, save_dir = "mock1"), + ModelSpec("mock2", fitter = zeroSum::zeroSum, save_dir = "mock2") + ) + expect_silent(new_msl <- prepend_to_save_dir(model_spec_list, "prepend_me")) + expect_equal(new_msl[[1]]$save_dir, "prepend_me/mock1") + expect_equal(new_msl[[2]]$save_dir, "prepend_me/mock2") +})