diff --git a/DESCRIPTION b/DESCRIPTION index fa81df8b6..6562deae0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling and Statistical Transformations -Version: 0.8.0 +Version: 0.8.0.1 Authors@R: c( person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut", comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), diff --git a/NEWS.md b/NEWS.md index 3b15bb71e..3622c1738 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# datawizard (devel) + +BUG FIXES + +* Fixed issues in `data_write()` when writing labelled data into SPSS format + and vectors were of different type as value labels. + # datawizard 0.8.0 BREAKING CHANGES diff --git a/R/data_write.r b/R/data_write.r index 8a868d7ae..b8d710d2e 100644 --- a/R/data_write.r +++ b/R/data_write.r @@ -127,18 +127,30 @@ data_write <- function(data, insight::format_alert("Preparing data file: converting variable types.") } x[] <- lapply(x, function(i) { + # make sure we have labelled class for labelled data value_labels <- attr(i, "labels", exact = TRUE) variable_label <- attr(i, "label", exact = TRUE) + # factor requires special preparation to save levels as labels + # haven:::vec_cast_named requires "x" and "labels" to be of same type if (is.factor(i)) { - # factor requires special preparation to save levels as labels haven::labelled( x = as.numeric(i), labels = stats::setNames(seq_along(levels(i)), levels(i)), label = variable_label ) } else if (!is.null(value_labels) || !is.null(variable_label)) { - # make sure we have labelled class for labelled data - haven::labelled(x = i, labels = value_labels, label = variable_label) + # character requires special preparation to save value labels + # haven:::vec_cast_named requires "x" and "labels" to be of same type + if (is.character(i)) { + haven::labelled( + x = i, + labels = stats::setNames(as.character(value_labels), names(value_labels)), + label = variable_label + ) + } else { + # this should work for the remaining types... + haven::labelled(x = i, labels = value_labels, label = variable_label) + } } else { # non labelled data can be saved "as is" i diff --git a/tests/testthat/test-data_write.R b/tests/testthat/test-data_write.R index 42cc63e9f..a51931bd7 100644 --- a/tests/testthat/test-data_write.R +++ b/tests/testthat/test-data_write.R @@ -30,6 +30,29 @@ test_that("data_write, SPSS", { }) +tmp <- tempfile(fileext = ".sav") +on.exit(unlink(tmp)) + +test_that("data_write, SPSS, mixed types of labelled vectors", { + d <- data.frame( + a = 1:3, + b = letters[1:3], + c = factor(letters[1:3]), + d = as.Date(c("2022-01-01", "2022-02-01", "2022-03-01")), + e = c(TRUE, FALSE, FALSE), + stringsAsFactors = FALSE + ) + + # Date and Logical cannot be labelled + d$a <- assign_labels(d$a, variable = "First", values = c("one", "two", "three")) + d$b <- assign_labels(d$b, variable = "Second", values = c("A", "B", "C")) + d$c <- assign_labels(d$c, variable = "Third", values = c("ey", "bee", "see")) + + # expect message, but no error + expect_message(data_write(d, "test.sav"), regex = "Preparing") +}) + + # Stata -------------------------------------