Skip to content

Commit

Permalink
Merge pull request #2 from ratschlab/allow-dir-of-jsons-for-data-sources
Browse files Browse the repository at this point in the history
Allow passing a directory containing jsons to data-sources arg
  • Loading branch information
mlondschien authored Mar 4, 2024
2 parents e79ef46 + a06017f commit aea5685
Show file tree
Hide file tree
Showing 10 changed files with 10,918 additions and 9,933 deletions.
20 changes: 18 additions & 2 deletions R/utils-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ user_config_path <- function() {
config_paths <- function() c(user_config_path(), default_config_path())

#' @param name File name of the configuration file (`.json` will be appended)
#' or name of a folder containing multiple configuration files.
#' @param cfg_dirs Character vector of directories searched for config files
#' @param combine_fun If multiple files are found, a function for combining
#' returned lists
Expand All @@ -275,8 +276,23 @@ get_config <- function(name, cfg_dirs = config_paths(), combine_fun = c, ...) {
assert_that(is.string(name), has_length(cfg_dirs), all_fun(cfg_dirs, is.dir),
null_or(combine_fun, is.function))

res <- lapply(file.path(cfg_dirs, paste0(name, ".json")),
read_if_exists, ...)
res <- list()

for (dir in cfg_dirs) {
dir_json <- file.path(dir, paste0(name, ".json"))
if (isTRUE(file.exists(dir_json))) {
res <- append(res, list(read_if_exists(dir_json, ...)))
}

if (isTRUE(dir.exists(file.path(dir, name)))) {
for (dir_json in list.files(path = file.path(dir, name),
pattern = "\\.json$",
full.names = TRUE,
recursive = TRUE)) {
res <- append(res, list(read_if_exists(dir_json, ...)))
}
}
}

if (is.null(combine_fun)) {
res
Expand Down
Loading

0 comments on commit aea5685

Please sign in to comment.