-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from VForWaTer/seperate_parameters_and_data
Seperate parameters and data
- Loading branch information
Showing
6 changed files
with
186 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#' @title get_data | ||
#' | ||
#' @description | ||
#' This package is intended to be used inside a docker container to parse | ||
#' data from "/in/input.json" with the help of | ||
#' the input configuration in "/src/tool.yml". | ||
#' The data can be parsed by file extension. | ||
#' If return_data_paths is set to FALSE, the function will return the paths | ||
#' to the data files. If set to TRUE, the function will try to parse the data. | ||
#' | ||
#' @return A list of the parsed data specified in "/in/input.json" | ||
#' @examples | ||
#' \dontrun{ | ||
#' data <- get_data() | ||
#' } | ||
#' @export | ||
#' @importFrom jsonlite read_json | ||
#' @importFrom yaml read_yaml | ||
#' @importFrom tools file_ext | ||
#' @importFrom utils read.csv read.table | ||
|
||
get_data <- function(return_data_paths = FALSE) { | ||
# get the input file env variable | ||
PARAM_FILE <- Sys.getenv(x = "PARAM_FILE") | ||
if (PARAM_FILE == "") { | ||
PARAM_FILE <- "/in/input.json" | ||
} | ||
|
||
# get the config file env variable | ||
CONF_FILE <- Sys.getenv(x = "CONF_FILE") | ||
if (CONF_FILE == "") { | ||
CONF_FILE <- "/src/tool.yml" | ||
} | ||
|
||
# get the tool name | ||
TOOL <- tolower(Sys.getenv(x = "TOOL_RUN")) | ||
|
||
# parse the json, access data section | ||
data <- jsonlite::read_json(path = PARAM_FILE, simplifyVector = TRUE)[[TOOL]][["data"]] | ||
|
||
# parse the config yaml, access data section | ||
data_config <- yaml::read_yaml(CONF_FILE)$tools[[TOOL]]$data | ||
|
||
# initialize list for data to return | ||
data_return <- list() | ||
|
||
# parse data | ||
for (name in names(data)) { | ||
# get the file path | ||
path <- data[[name]] | ||
|
||
# get the file extension | ||
ext <- tolower(tools::file_ext(path)) | ||
# add a dot at the beginning | ||
ext <- paste(".", ext, sep = "") | ||
|
||
# check if the dataset is given with the correct extension | ||
if ("extension" %in% names(data_config[[name]])) { | ||
if (!(ext %in% data_config[[name]]$extension)) { | ||
stop(paste("The file extension '", ext, "' is not contained in [", paste(data_config[[name]]$extension, collapse = " "), "]", sep = "")) | ||
} | ||
} | ||
|
||
# check if the file exists | ||
if (!file.exists(path)) { | ||
stop(paste("The file '", path, "' does not exist.")) | ||
} | ||
|
||
# do not parse files by extension if parse_files is FALSE | ||
if (return_data_paths) { | ||
data_return[[name]] <- path | ||
next | ||
} | ||
|
||
# .dat | ||
if (ext == ".dat") { | ||
parsed <- as.matrix(read.table(path)) # matrix files: no header, no index | ||
dimnames(parsed) <- NULL | ||
} | ||
|
||
# .csv | ||
else if (ext == ".csv") { | ||
parsed <- read.csv(path) | ||
} | ||
|
||
# not supported to parse | ||
else { | ||
parsed <- path | ||
} | ||
|
||
# append value to parsed_params | ||
data_return[[name]] <- parsed | ||
} | ||
return(data_return) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
test_that("parse data works", { | ||
# set environment variables for input files and the TOOL to use | ||
Sys.setenv("PARAM_FILE" = testthat::test_path("testdata/in", "parameters.json")) | ||
Sys.setenv("CONF_FILE" = testthat::test_path("testdata/src", "tool.yml")) | ||
Sys.setenv("TOOL_RUN" = "foobar") | ||
|
||
# get the data | ||
data <- get_data() | ||
|
||
# test parsed data | ||
testthat::expect_length(data, 2) | ||
testthat::expect_equal(data$foo_matrix[3, 2], 6.1) | ||
testthat::expect_equal(colnames(data$foo_csv), c("A", "B", "C", "D")) | ||
|
||
# get data paths | ||
data_paths <- get_data(return_data_paths = TRUE) | ||
|
||
# test that data paths are returned | ||
testthat::expect_length(data_paths, 2) | ||
testthat::expect_equal(data_paths$foo_matrix, "testdata/in/foo_matrix.dat") | ||
testthat::expect_equal(data_paths$foo_csv, "testdata/in/foo_csv.csv") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
{ | ||
"foobar": { | ||
"foo_int": 42, | ||
"foo_float": 13.37, | ||
"foo_string": "Never eat yellow snow", | ||
"foo_enum": "bar", | ||
"foo_array": [34, 55, 23, 43, 23], | ||
"foo_matrix": "testdata/in/foo_matrix.dat", | ||
"foo_csv": "testdata/in/foo_csv.csv" | ||
"parameters": { | ||
"foo_int": 42, | ||
"foo_float": 13.37, | ||
"foo_string": "Never eat yellow snow", | ||
"foo_enum": "bar", | ||
"foo_array": [34, 55, 23, 43, 23], | ||
"foo_boolean": true | ||
}, | ||
"data": { | ||
"foo_matrix": "testdata/in/foo_matrix.dat", | ||
"foo_csv": "testdata/in/foo_csv.csv" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters