Skip to content

Commit

Permalink
Fix data_write() for partially labelled characters (#459)
Browse files Browse the repository at this point in the history
* fix data_write for partially labelled characters

* remove whitespace

* Update tests/testthat/test-data_write.R

Co-authored-by: Etienne Bacher <[email protected]>

* use correct file path

* read back and check

---------

Co-authored-by: Etienne Bacher <[email protected]>
  • Loading branch information
strengejacke and etiennebacher authored Sep 14, 2023
1 parent 2989935 commit 6e93950
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.8.0.13
Version: 0.8.0.14
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ BUG FIXES
* Fixed issues in `data_write()` when writing labelled data into SPSS format
and vectors were of different type as value labels.

* Fixed issues in `data_write()` when writing labelled data into SPSS format
for character vectors with missing value labels, but existing variable
labels.

* Fixed issue in `recode_into()` with probably wrong case number printed in the
warning when several recode patterns match to one case.

Expand Down
6 changes: 5 additions & 1 deletion R/data_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,13 @@ data_write <- function(data,
# 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)) {
# only prepare value labels when these are not NULL
if (!is.null(value_labels)) {
value_labels <- stats::setNames(as.character(value_labels), names(value_labels))
}
haven::labelled(
x = i,
labels = stats::setNames(as.character(value_labels), names(value_labels)),
labels = value_labels,
label = variable_label
)
} else {
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-data_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,23 @@ test_that("data_write, no file extension", {
expect_error(data_write(d, "mytestfile"))
expect_error(data_write(d, NULL))
})


# writing character vector works for missing value labels ------------------

tmp <- tempfile(fileext = ".sav")
on.exit(unlink(tmp))

test_that("data_write, existing variable label but missing value labels", {
d <- data.frame(
a = letters[1:3],
stringsAsFactors = FALSE
)
d$a <- assign_labels(d$a, variable = "First")
# expect message, but no error
expect_message(data_write(d, tmp), regex = "Preparing")

# check if data is really the same
d2 <- data_read(tmp)
expect_identical(d2, d)
})

0 comments on commit 6e93950

Please sign in to comment.