Skip to content

Commit

Permalink
Better error message when data file is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinCCYap committed Jan 19, 2022
1 parent c97dbdf commit e4b2793
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ importFrom(flipU,AllVariablesNames)
importFrom(flipU,CopyAttributes)
importFrom(flipU,EscapeRegexSymbols)
importFrom(flipU,HasOutcome)
importFrom(flipU,InterceptExceptions)
importFrom(flipU,OutcomeName)
importFrom(flipU,RemoveAt)
importFrom(flipU,TrimWhitespace)
Expand Down
23 changes: 20 additions & 3 deletions R/mergingandstackingutilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ readDataSets <- function(data.set.names, min.data.sets = 1)
#' @return A list of data frames, with each representing a data set.
#' @noRd
#' @importFrom haven read_sav
readLocalDataSets <- function(data.set.paths)
#' @importFrom flipU InterceptExceptions
readLocalDataSets <- function(data.set.paths, parser = read_sav)
{
result <- lapply(data.set.paths, function(path) {
read_sav(path)
handler = createReadErrorHandler(path)
InterceptExceptions(parser(path), error.handler = handler)
})
names(result) <- basename(data.set.paths)
result
Expand All @@ -45,11 +47,26 @@ readLocalDataSets <- function(data.set.paths)
#' @importFrom flipAPI QLoadData
readDataSetsFromDisplayrCloudDrive <- function(data.set.names)
{
result <- lapply(data.set.names, QLoadData)
result <- lapply(data.set.names, function(nm) {
handler = createReadErrorHandler(nm)
InterceptExceptions(QLoadData(nm), error.handler = handler)
})
names(result) <- data.set.names
result
}

createReadErrorHandler <- function(data.set.name)
{
function(e) {
if (grepl("Invalid file, or file has unsupported features", e$message)) {
stop(paste0("The data set '", data.set.name, "' could not be parsed. ",
"Check the data set for issues and try again after fixing them or removing unnecessary variables."))
} else {
stop(e$message)
}
}
}

#' @param data.set A data frame containing the data set to write.
#' @param data.set.name A character scalar of data file name to write to.
#' @return Nothing.
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-mergingandstackingutilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,12 @@ test_that("readDataSets: Non .sav files throw nice error", {
"Only SPSS .sav data files are accepted."))
})

test_that("readDataSets: better error message when data file is invalid", {
mock.parser <- function(x) stop("Invalid file, or file has unsupported features")
expect_error(readLocalDataSets("bad.sav", mock.parser),
paste0("The data set 'bad.sav' could not be parsed. ",
"Check the data set for issues and try again after fixing them or removing unnecessary variables."))
})

if (file.exists("Combined data set.sav"))
file.remove("Combined data set.sav")

0 comments on commit e4b2793

Please sign in to comment.