Skip to content

Commit

Permalink
Bug Fix Reload Function (#21)
Browse files Browse the repository at this point in the history
* changed global access of object in reload

* pre-commit autofixes

* included reload funciton instead of method

* added namespace

* updated changelog and bumped version

---------

Co-authored-by: Tom Schwarzl <[email protected]>
Co-authored-by: DSchreyer <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent a10afdf commit 1ccb35e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 75 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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.1

### Improvements

- Fixed `reload()` function for dsoParams

## v0.5.0

### New Features
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.5.0
Version: 0.5.1
Author: Daniel Schreyer<[email protected]>,
Gregor Sturm<[email protected]>,
Thomas Schwarzl<[email protected]>
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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: 0 additions & 27 deletions R/class-dsoParams.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,3 @@ 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)
})
61 changes: 34 additions & 27 deletions R/read_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,64 @@
#' @importFrom yaml read_yaml
#' @export
read_params <- function(stage_path = NULL, return_list = FALSE) {
if(!is.logical(return_list))
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)) {

if (!is.null(stage_path)) {
# first check for input validity
if(!is.character(stage_path))
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)) {
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))
message(glue::glue("Reloading from already set stage_path: {config_env$stage_dir}"))
stage_path <- config_env$stage_dir
}
}

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

tryCatch({
output <- system2(DSO_EXEC,
c("get-config",
shQuote(stage_path)),
stdout = tmp_config_file,
stderr = tmp_err_file)
tryCatch(
{
output <- system2(DSO_EXEC,
c(
"get-config",
shQuote(stage_path)
),
stdout = tmp_config_file,
stderr = tmp_err_file
)

stderror <- readLines(tmp_err_file)
stderror <- readLines(tmp_err_file)

# error handling, display only rows after first error
# for better readability
if (output != 0 || any(grepl("ERROR", stderror))) {
stop(
paste("An error occurred when executing dso get-config:\n",
paste(
stderror[which(grepl("ERROR", stderror)) : length(stderror)],
collapse = "\n"
)
# error handling, display only rows after first error
# for better readability
if (output != 0 || any(grepl("ERROR", stderror))) {
stop(
paste(
"An error occurred when executing dso get-config:\n",
paste(
stderror[which(grepl("ERROR", stderror)):length(stderror)],
collapse = "\n"
)
)
)
)
}
},
error = function(e) {
stop("An error occurred when executing dso get-config: ", e$message)
}
}, error = function(e) {
stop("An error occurred when executing dso get-config: ", e$message)
})
)

yaml <- read_yaml(tmp_config_file)
unlink(tmp_config_file)
Expand Down
17 changes: 17 additions & 0 deletions R/reload.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#' @title Reload dsoParams
#' @description
#' Reloads the current dsoParams configuration into the object.
#'
#' @param params dsoParams object
#' @param env environment in which object is located, caller_env() by default
#' @return The updated dsoParams object.
#' @export
reload <- function(params, env = caller_env()) {
if (!inherits(params, "dsoParams")) {
stop("The object is not of class 'dsoParams'")
}
var_name <- deparse(substitute(params))
assign(var_name, value = read_params(), envir = env)

invisible(get(var_name, envir = env))
}
14 changes: 0 additions & 14 deletions man/reload-dsoParams-method.Rd

This file was deleted.

15 changes: 10 additions & 5 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 1ccb35e

Please sign in to comment.