Skip to content

Commit

Permalink
Treat NaN values the same as NA values
Browse files Browse the repository at this point in the history
Previously NaN values were always output as NaN, this makes them use the
value of the `na` argument, which is behavior consistent with
write.csv() and data.table::fwrite().

Fixes #1082
  • Loading branch information
jimhester committed Mar 13, 2020
1 parent 468eb5c commit ccc4dea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# readr (development version)

## Breaking changes

* `write_*()` functions now output any NaN values in the same way as NA values,
controlled by the `na=` argument. (#1082).

## Additional features and fixes

* `write_excel_csv()` no longer outputs a byte order mark when appending to a file (#1075).

* The `read_*` functions now close properly all connections, including on
Expand Down
4 changes: 1 addition & 3 deletions src/write_delim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,8 @@ void stream_delim(
case REALSXP: {
double value = REAL(x)[i];
if (!R_FINITE(value)) {
if (ISNA(value)) {
if (ISNA(value) || ISNAN(value)) {
output << na;
} else if (ISNAN(value)) {
output << "NaN";
} else if (value > 0) {
output << "Inf";
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test_that("read_delim/csv/tsv and write_delim round trip special chars", {

test_that("special floating point values translated to text", {
df <- data.frame(x = c(NaN, NA, Inf, -Inf))
expect_equal(format_csv(df), "x\nNaN\nNA\nInf\n-Inf\n")
expect_equal(format_csv(df), "x\nNA\nNA\nInf\n-Inf\n")
})

test_that("logical values give long names", {
Expand Down

0 comments on commit ccc4dea

Please sign in to comment.