diff --git a/CHANGELOG.md b/CHANGELOG.md index dbd49ca..821f52b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/DESCRIPTION b/DESCRIPTION index 2058a76..ca2b966 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: dso Type: Package Title: dso R companion package -Version: 0.4.4 +Version: 0.5.0 Author: Daniel Schreyer, Gregor Sturm, Thomas Schwarzl diff --git a/NAMESPACE b/NAMESPACE index c1781bf..c49e432 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/class-dsoParams.R b/R/class-dsoParams.R index 22a15bb..5f27b5f 100644 --- a/R/class-dsoParams.R +++ b/R/class-dsoParams.R @@ -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) +}) diff --git a/R/read_params.R b/R/read_params.R index 67c6250..d446784 100644 --- a/R/read_params.R +++ b/R/read_params.R @@ -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() diff --git a/README.md b/README.md index 40744b7..8812b9e 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/man/read_params.Rd b/man/read_params.Rd index cf5bb12..14afeff 100644 --- a/man/read_params.Rd +++ b/man/read_params.Rd @@ -4,10 +4,10 @@ \alias{read_params} \title{read_params} \usage{ -read_params(stage_path, return_list = FALSE) +read_params(stage_path = NULL, return_list = FALSE) } \arguments{ -\item{stage_path}{relative path to stage directory from project root} +\item{stage_path}{relative path to stage directory from project root, when NULL, already set stage path will be taken from the config_environment} \item{return_list}{returns a list if TRUE, by default it return `dsoParams` class which is a list with secure access} } diff --git a/man/reload-dsoParams-method.Rd b/man/reload-dsoParams-method.Rd new file mode 100644 index 0000000..e50e723 --- /dev/null +++ b/man/reload-dsoParams-method.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-dsoParams.R +\name{reload,dsoParams-method} +\alias{reload,dsoParams-method} +\title{reload dso params} +\usage{ +\S4method{reload}{dsoParams}(object) +} +\arguments{ +\item{object}{dsoParams object} +} +\description{ +reloads the current dsoParams config into object +} diff --git a/man/reload.Rd b/man/reload.Rd new file mode 100644 index 0000000..7fd4b29 --- /dev/null +++ b/man/reload.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-dsoParams.R +\name{reload} +\alias{reload} +\title{reload function} +\usage{ +reload(object, ...) +} +\arguments{ +\item{object}{dsoParams config object} +} +\description{ +Generic for function reload +}