Skip to content

Commit

Permalink
add: function to read import definitions using system variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed May 6, 2024
1 parent 53eb3af commit 465a893
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export(pretty_print_updates)
export(print_last_used)
export(ratio_df_columns)
export(read_documentation)
export(read_import_definitions)
export(readrds_csv)
export(regex_content_parameter)
export(retrieve_data_metadata)
Expand Down
35 changes: 35 additions & 0 deletions R/read_documentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ read_documentation <- function(filename = "", documentation_location = NULL, rea
}
}

#' read_import_definitions
#'
#' A function that reads an import definitions file.
#'
#' @param filename The filename of the import definitions file.
#' @param metadata_location Location of the import definitions files,
#' if NULL system variables will be used.
#' @param ... Additional parameters to be passed to the file reading function.
#'
#' @return A data frame containing the import definitions.
#' @export
read_import_definitions <- function(filename = "", metadata_location = NULL, ...) {
if (is.null(metadata_location)) {
if (!Sys.getenv("METADATA_IMPORT_DEFINITIONS_DIR") == "") {
metadata_location <- Sys.getenv("METADATA_IMPORT_DEFINITIONS_DIR")
} else {
stop("System variable for metadata_location is missing.")
}
}

file_path <- file.path(metadata_location, filename)

# Determine the file reading function based on the file extension
file_extension <- tools::file_ext(filename)
if (file_extension == "csv") {
return(utils::read.csv(file_path, stringsAsFactors = FALSE, ...))
} else if (file_extension == "txt" || file_extension == "dat") {
# Assume tab-separated file
return(utils::read.delim(file_path, stringsAsFactors = FALSE, ...))
} else {
stop("Unsupported file format. Only CSV, TXT, and DAT files are supported.")
}
}


#' Overwrite the arguments with the specified dots (...)
#'
#' This function can be used within a function to ensure that
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ reference:
- get_analysisset_7
- get_archived_result_files
- read_documentation
- read_import_definitions

- title: Snippets methods.
desc: |
Expand Down
22 changes: 22 additions & 0 deletions man/read_import_definitions.Rd

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

0 comments on commit 465a893

Please sign in to comment.