Skip to content

Commit

Permalink
write and test prepend_to_save_dir()
Browse files Browse the repository at this point in the history
  • Loading branch information
lgessl committed Dec 11, 2023
1 parent e7bce3d commit 72ea59b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions R/data_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
23 changes: 23 additions & 0 deletions man/prepend_to_save_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-data_spec.R
Original file line number Diff line number Diff line change
@@ -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")
})

0 comments on commit 72ea59b

Please sign in to comment.