Skip to content

Commit 0aabdb4

Browse files
authored
Handle labelled values with no labels (#609)
Fixes #606
1 parent 102d830 commit 0aabdb4

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# haven (development version)
22

3+
* `write_dta()` now correctly handles "labelled"-class numeric (double) variables
4+
that don't have value labels (@jmobrien, #606, #609).
5+
36
* `write_dta()` now allows variable names up to 32 characters (@sbae, #605).
47

58
* Fix issue with `read_dta()` crashing R when StrL variables with missing values

R/haven-stata.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ validate_dta_label <- function(label) {
139139
# helpers -----------------------------------------------------------------
140140

141141
has_non_integer_labels <- function(x) {
142+
if (is.null(attr(x, "labels"))) {
143+
return(FALSE)
144+
}
145+
142146
if (!is.labelled(x)) {
143147
return(FALSE)
144148
}

tests/testthat/test-haven-stata.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ test_that("can write labelled with NULL labels", {
164164
chr <- labelled(c("a", "b"), NULL)
165165

166166
expect_equal(roundtrip_var(int, "dta"), c(1L, 2L))
167+
expect_equal(roundtrip_var(num, "dta"), c(1L, 2L))
167168
expect_equal(roundtrip_var(chr, "dta"), c("a", "b"))
168169
})
169170

0 commit comments

Comments
 (0)