Skip to content

Commit

Permalink
Implementation of reload function (#18)
Browse files Browse the repository at this point in the history
* added reload function for dsoParams

* version bump

* added export

* implementation of setMethod for reload

* fixed error check and added additional explanation

* err stage_dir/ stage_path

* fixed stage_path after reload

* updated readme

* pre-commit autofixes

* Update CHANGELOG.md

version bump

* Update DESCRIPTION

version bump to 0.5.0

* description update

* pre-commit autofixes

---------

Co-authored-by: Tom Schwarzl <[email protected]>
Co-authored-by: tschwarzl <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 8f5e0b3 commit a10afdf
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.5.0

### New Features

- Introduced `reload()` function for dsoParams

## v0.4.4

### Improvements

- Improved error handling for `read_params()`

## v0.4.3
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: dso
Type: Package
Title: dso R companion package
Version: 0.4.4
Version: 0.5.0
Author: Daniel Schreyer<[email protected]>,
Gregor Sturm<[email protected]>,
Thomas Schwarzl<[email protected]>
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export(create_stage)
export(dsoParams)
export(init)
export(read_params)
export(reload)
export(safe_get)
export(set_stage)
export(stage_here)
exportClasses(dsoParams)
exportMethods(as.list)
exportMethods(reload)
exportMethods(show)
importFrom(glue,glue)
importFrom(here,here)
Expand Down
27 changes: 27 additions & 0 deletions R/class-dsoParams.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,30 @@ setMethod(
})
}
)


#' @title reload function
#' @description
#'
#' Generic for function reload
#'
#' @param object dsoParams config object
#' @export
setGeneric("reload", function(object,...) standardGeneric("reload"))


#' @title reload dso params
#' @description
#' reloads the current dsoParams config into object
#'
#' @param object dsoParams object
#' @export
setMethod("reload", "dsoParams", function(object) {
if (!inherits(object, "dsoParams")) {
stop("The object is not of class 'dsoParams'")
}

object <<- read_params()

invisible(object)
})
29 changes: 26 additions & 3 deletions R/read_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,37 @@
#' is any subdirectory of the project root). The function recompiles params.in.yaml to params.yaml on-the-fly
#' to ensure that up-to-date params are always loaded.
#'
#' @param stage_path relative path to stage directory from project root
#' @param stage_path relative path to stage directory from project root, when NULL, already set stage path will be taken from the config_environment
#' @param return_list returns a list if TRUE, by default it return `dsoParams` class which is a list with secure access
#'
#' @return parameters as list of list as `dsoParams` or conventional list when `return_list` is set.
#' @importFrom yaml read_yaml
#' @export
read_params <- function(stage_path, return_list = FALSE) {
stage_path <- set_stage(stage_path)
read_params <- function(stage_path = NULL, return_list = FALSE) {
if(!is.logical(return_list))
stop("Argument return_list needs to be logical.")

# if stage_path argument was path, set stage from that argument
if(!is.null(stage_path)) {

# first check for input validity
if(!is.character(stage_path))
stop("stage_path argument must be a character string or NULL to reload the config")

# then set stage path
stage_path <- set_stage(stage_path)

} else {
# stage_path argument is null, therefore not set. Check if stage_dir
# has been already set in config_env, if yes reload, if not, stop with error
if(is.null(config_env$stage_dir)) {
stop("stage_path argument missing.")
} else {
cat(paste("reloading from already set stage_path:", config_env$stage_dir))
stage_path <- config_env$stage_dir
}
}

tmp_config_file <- tempfile()
tmp_err_file <- tempfile()

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
The purpose of this package is to provide access to files and configuration organized in a dso project. It provides two main functions:

- `read_params(stage_path)` loads the configuration for the specified stage into an `dsoParams` R object which can be accessed like a list. The stage name must be relative to the project root. This function works independent of the current working directory, as long as you are in any subdirectory of the project.
- `reload(object)` reloads the config loaded by read_params. Usage: `reload(params)`
- `stage_here(rel_path)` is inspired by [here()](https://here.r-lib.org/). While `here()` resolves paths that are relative to the project root `stage_here()` resolves paths that are relative to the stage specified in `read_params`.
- `safe_get(params$samplesheet)` is a helper function assuring that a nested list call does not return NULL if accessed incorrectly. ' safe_get() will produce an error and point to the incorrectly accessed slot.
- `safe_get()` is a helper function assuring that a nested list call does not return NULL if accessed incorrectly. ' safe_get() will produce an error and point to the incorrectly accessed slot.

Additionally, `dso-r` provides an R interface to some of the most important CLI commands of `dso`.

Expand Down
4 changes: 2 additions & 2 deletions man/read_params.Rd

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

14 changes: 14 additions & 0 deletions man/reload-dsoParams-method.Rd

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

14 changes: 14 additions & 0 deletions man/reload.Rd

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

0 comments on commit a10afdf

Please sign in to comment.