Skip to content

Commit

Permalink
Treat missing values as length 0 when calculating user width (#701)
Browse files Browse the repository at this point in the history
Close #699
  • Loading branch information
gorcha authored Dec 14, 2022
1 parent be597a3 commit f0caeb4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# haven (development version)

* Fix bug in string variable width calculation that treated `NA` values as width
2. `NA` values are now treated as blanks for width calculations (#699).

# haven 2.5.1

* All `labelled()` vectors now have left-aligned column headers when printing
Expand Down
10 changes: 9 additions & 1 deletion src/DfWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ inline const bool string_is_missing(SEXP x, int i) {
return STRING_ELT(x, i) == NA_STRING;
}

inline const int string_len_missing(SEXP x, int i) {
if (string_is_missing(x, i)) {
return 0;
} else {
return strlen(string_utf8(x, i));
}
}


inline readstat_measure_e measureType(SEXP x) {
if (Rf_inherits(x, "ordered")) {
Expand Down Expand Up @@ -366,7 +374,7 @@ class Writer {
int user_width = userWidth(x);
int max_length = 1;
for (int i = 0; i < x.size(); ++i) {
int length = strlen(string_utf8(x, i));
int length = string_len_missing(x, i);
if (length > max_length)
max_length = length;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/haven-sas.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
Error:
! Failed to create file: A provided name contains an illegal character.

# user width warns appropriately when data is wider than value

Code
write_xpt(df, path)
Condition
Warning:
Column `b` contains string values longer than user width 1. Width set to 2 to accommodate.

12 changes: 12 additions & 0 deletions tests/testthat/test-haven-sas.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,15 @@ test_that("can roundtrip format attribute", {

expect_identical(df, out)
})

test_that("user width warns appropriately when data is wider than value", {
df <- tibble(
a = c("a", NA_character_),
b = c("b", "NA"),
)
attr(df$a, "width") <- 1
attr(df$b, "width") <- 1

path <- tempfile()
expect_snapshot(write_xpt(df, path))
})

0 comments on commit f0caeb4

Please sign in to comment.