Skip to content

Commit

Permalink
can import from multiple hierarchy zip if hierarchy is set
Browse files Browse the repository at this point in the history
  • Loading branch information
rix133 committed Nov 5, 2024
1 parent 78c6a42 commit 2dc0129
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion R/createRDBESDataObject.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ createRDBESDataObject <- function(input = NULL,
# -------------------------------------------------------------------------

if(import.type == "zip") output <- importRDBESDataZIP(filenames = input,
castToCorrectDataTypes = castToCorrectDataTypes)
castToCorrectDataTypes = castToCorrectDataTypes, ...)

if(import.type == "csv") output <- importRDBESDataCSV(rdbesExtractPath = input,
listOfFileNames = listOfFileNames,
Expand Down
28 changes: 23 additions & 5 deletions R/importRDBESDataZIP.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#' attempt to cast the required columns to the correct data type. If `FALSE`
#' then the column data types will be determined by how the csv files are read
#' in. Default is `TRUE`.
#' @param Hierarchy - Optional. A number specifying the hierarchy of the data
#' to be imported. If the .zip file contains multiple hierarchies.
#'
#' @return a list of all the RDBES data tables The table that are not in input
#' data are NULL
Expand All @@ -25,7 +27,8 @@
#' }

importRDBESDataZIP <- function(filenames,
castToCorrectDataTypes = TRUE) {
castToCorrectDataTypes = TRUE,
Hierarchy = NULL) {

# Generates random number for the temp import folder name
randInt <- paste0(sample(1:100, 3), collapse = "")
Expand All @@ -44,7 +47,8 @@ importRDBESDataZIP <- function(filenames,
unzipped <- unzipped[grep("*.csv", unzipped)]
intersected <- intersect(unzipped, all_unzipped)
if(length(intersected) != 0) {
warning(paste0("Duplicate unzipped files detected:\n", paste0("\t", intersected, "\n", collapse="\n")))
warning(paste0("Duplicate unzipped files detected:\n",
paste0("\t", intersected, "\n", collapse="\n")))
}
all_unzipped <<- c(all_unzipped, unzipped)
return(unzipped)
Expand All @@ -59,9 +63,23 @@ importRDBESDataZIP <- function(filenames,
if(length(dirs) > 0) {
hdirs <- dirs[grepl("H[0-9]+", dirs)]
if(length(hdirs) > 1) {
stop("You cannot import a mix of different hierarchies in one 'zip' ",
"input. To import multiple tables unzip all files and import as ",
"a folder of 'csv' files.")
valid_hierarchies <- as.numeric(gsub("H", "", hdirs))
example <- paste0("Hierachy = ", valid_hierarchies[1])
if(is.null(Hierarchy)) {
stop("The zip file contains multiple hierarchies.\n",
"To import a selected hierarchy, please provide the hierarchy ",
"as an argument e.g like:\n",example)
}
else if(!Hierarchy %in% valid_hierarchies) {
stop("The zip file does not contain the hierarchy specified. ",
"The options are: ", paste0(valid_hierarchies, collapse = ", "),
"\nPlease provide a valid hierarchy as an argument. e.g like:\n",
example)

}
else {
dirs <- c(setdiff(dirs, hdirs), paste0("H", Hierarchy))
}
}
#remove directory structure
for(d in dirs){
Expand Down
6 changes: 4 additions & 2 deletions man/importRDBESDataZIP.Rd

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

29 changes: 28 additions & 1 deletion tests/testthat/test-createRDBESDataObject.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,34 @@ capture.output({ ## suppresses printing of console output when running test()
genObj <- expect_error(
createRDBESDataObject(paste0(dirH1, zipFiles),
castToCorrectDataTypes = TRUE),
"You cannot import a mix of different hierarchies in one 'zip' input. To import multiple tables unzip all files and import as a folder of 'csv' files."
"The zip file contains multiple hierarchies.\nTo import a selected hierarchy, please provide the hierarchy as an argument e.g like:\nHierachy = 1"
)

})

test_that("importing foldered zipped H1 and H2 example data works when hierarchy is specified", {
zipFiles <- c(
"H1_H2_2023_10_16_fd.zip"
)

genObj <- createRDBESDataObject(paste0(dirH1, zipFiles),
castToCorrectDataTypes = TRUE,
Hierarchy = 1)

expect_equal(genObj, expObjH1)

})

test_that("importing foldered zipped H1 and H2 example does not work when hierarchy is misspecified", {
zipFiles <- c(
"H1_H2_2023_10_16_fd.zip"
)

genObj <- expect_error(
createRDBESDataObject(paste0(dirH1, zipFiles),
castToCorrectDataTypes = TRUE,
Hierarchy = "H1"),
"The zip file does not contain the hierarchy specified. The options are: 1, 2\nPlease provide a valid hierarchy as an argument. e.g like:\nHierachy = 1"
)

})
Expand Down

0 comments on commit 2dc0129

Please sign in to comment.