Skip to content

Commit

Permalink
RS-9776: Better error message when input data file for merging and st…
Browse files Browse the repository at this point in the history
…acking is invalid (#22)

* Better error message when data file is invalid

* Wording change [revdev skip]

* Bump version
  • Loading branch information
JustinCCYap committed Jan 19, 2022
1 parent c97dbdf commit e45b6f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: flipData
Type: Package
Title: Functions for extracting and describing data
Version: 1.5.5
Version: 1.5.6
Author: Displayr <opensource@displayr.com>
Maintainer: Displayr <opensource@displayr.com>
Description: Functions for extracting data from formulas and
Expand Down
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 file '", 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 file '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 e45b6f2

Please sign in to comment.