Skip to content

Commit

Permalink
Further protection for haven-labelled text variables
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfacer committed Dec 21, 2022
1 parent 806317e commit de6fe75
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 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.6.5
Version: 1.6.6
Author: Displayr <opensource@displayr.com>
Maintainer: Displayr <opensource@displayr.com>
Description: Functions for extracting data from formulas and
Expand Down
14 changes: 13 additions & 1 deletion R/mergedatasetsbycase.R
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,7 @@ isMissingValue <- function(text)
# See unit tests in test-mergedatasetsbycase.R
isParsableAsNumeric <- function(text)
{
text <- unclassHavenLabelledTextIfNEcessary(text)
missing.indices <- isMissingValue(text)
all(!is.na(suppressWarnings(as.numeric(text[!missing.indices]))))
}
Expand All @@ -1712,8 +1713,19 @@ isParsableAsNumeric <- function(text)
#' @importFrom flipTime AsDate AsDateTime
isParsableAsDateTime <- function(text)
{
text <- unclassHavenLabelledTextIfNEcessary(text)
missing.indices <- isMissingValue(text)
all(!is.na(AsDateTime(as.character(text[!missing.indices]), on.parse.failure = "silent")))
all(!is.na(AsDateTime(text[!missing.indices], on.parse.failure = "silent")))
}

# Some spss files produce a weird format when parsed by Haven. It does not occur
# for files created e.g. in Q or Displayr. This function removes the additional
# haven classes when parsing such Text variables to ensure that downstream
# checks for their values can succeed.
unclassHavenLabelledTextIfNEcessary <- function(text) {
if ("haven_labelled" %in% class(text))
text <- as.character(text)
text
}

# Check if numeric values are stored as seconds from the epoch (1970/1/1).
Expand Down
Binary file added inst/testdata/bad.haven.text.numeric.check.rds
Binary file not shown.
11 changes: 10 additions & 1 deletion tests/testthat/test-mergedatasetsbycase.R
Original file line number Diff line number Diff line change
Expand Up @@ -1265,9 +1265,18 @@ test_that("DS-4002: works when file extension in upper case", {
match.by.variable.labels = FALSE), NA)
})

test_that("DS-4045 Don't fail on converting haven labeled text to date", {
test_that("DS-4045, DS-4149 Don't fail on converting haven labeled text variables", {
bad.variable <- readRDS(findInstDirFile("bad.haven.text.rds"))
expect_error(isParsableAsDateTime(bad.variable), NA)
expect_error(isMissingValue(bad.variable), NA)
expect_error(isParsableAsNumeric(bad.variable), NA)


bad.variable.2 <- readRDS(findInstDirFile("bad.haven.text.numeric.check.rds"))
expect_error(isParsableAsDateTime(bad.variable.2), NA)
expect_error(isMissingValue(bad.variable.2), NA)
expect_error(isParsableAsNumeric(bad.variable.2), NA)

})

if (file.exists("Combined data set.sav"))
Expand Down

0 comments on commit de6fe75

Please sign in to comment.