From 0aabdb4b0d8cb9335b635f9ada6f0d158fc37196 Mon Sep 17 00:00:00 2001 From: Joseph O'Brien Date: Fri, 30 Jul 2021 09:32:29 -0500 Subject: [PATCH] Handle labelled values with no labels (#609) Fixes #606 --- NEWS.md | 3 +++ R/haven-stata.R | 4 ++++ tests/testthat/test-haven-stata.R | 1 + 3 files changed, 8 insertions(+) diff --git a/NEWS.md b/NEWS.md index 8523a9e2..893478b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # haven (development version) +* `write_dta()` now correctly handles "labelled"-class numeric (double) variables + that don't have value labels (@jmobrien, #606, #609). + * `write_dta()` now allows variable names up to 32 characters (@sbae, #605). * Fix issue with `read_dta()` crashing R when StrL variables with missing values diff --git a/R/haven-stata.R b/R/haven-stata.R index 3ba30632..e3ec854f 100644 --- a/R/haven-stata.R +++ b/R/haven-stata.R @@ -139,6 +139,10 @@ validate_dta_label <- function(label) { # helpers ----------------------------------------------------------------- has_non_integer_labels <- function(x) { + if (is.null(attr(x, "labels"))) { + return(FALSE) + } + if (!is.labelled(x)) { return(FALSE) } diff --git a/tests/testthat/test-haven-stata.R b/tests/testthat/test-haven-stata.R index f63b80d1..b068a66a 100644 --- a/tests/testthat/test-haven-stata.R +++ b/tests/testthat/test-haven-stata.R @@ -164,6 +164,7 @@ test_that("can write labelled with NULL labels", { chr <- labelled(c("a", "b"), NULL) expect_equal(roundtrip_var(int, "dta"), c(1L, 2L)) + expect_equal(roundtrip_var(num, "dta"), c(1L, 2L)) expect_equal(roundtrip_var(chr, "dta"), c("a", "b")) })