From f80ad26de7db5760d390c1739fc0ca272589eabc Mon Sep 17 00:00:00 2001 From: Matt Dowle Date: Tue, 10 Aug 2021 01:34:58 -0600 Subject: [PATCH] option 3 --- NEWS.md | 2 +- R/fwrite.R | 3 +-- inst/tests/tests.Rraw | 38 +++++++++++++++++++++----------------- src/fwrite.c | 20 ++++++++++++-------- src/fwrite.h | 3 ++- src/fwriteR.c | 19 ++++++++++++++++--- 6 files changed, 53 insertions(+), 32 deletions(-) diff --git a/NEWS.md b/NEWS.md index d001c495b..fd75bce68 100644 --- a/NEWS.md +++ b/NEWS.md @@ -163,7 +163,7 @@ 30. `fread(file=URL)` now works rather than error `does not exist or is non-readable`, [#4952](https://github.com/Rdatatable/data.table/issues/4952). `fread(URL)` and `fread(input=URL)` worked before and continue to work. Thanks to @pnacht for reporting and @ben-schwen for the PR. -31. `fwrite(DF, row.names=TRUE)` where `DF` has specific integer rownames (e.g. using `rownames(DF) <- c(10L,20L,30L)`) would incorrectly ignore the integer rowname and write the row number instead, [#4957](https://github.com/Rdatatable/data.table/issues/4957). Thanks to @dgarrimar for reporting and @ColeMiller1 for the PR. Further, when quote='auto' and the rownames are integers, they are no longer quoted. +31. `fwrite(DF, row.names=TRUE)` where `DF` has specific integer rownames (e.g. using `rownames(DF) <- c(10L,20L,30L)`) would incorrectly ignore the integer rownames and write the row numbers instead, [#4957](https://github.com/Rdatatable/data.table/issues/4957). Thanks to @dgarrimar for reporting and @ColeMiller1 for the PR. Further, when quote='auto' and the rownames are integers (either default or specific), they are no longer quoted. ## NOTES diff --git a/R/fwrite.R b/R/fwrite.R index 74d127c87..c822b0567 100644 --- a/R/fwrite.R +++ b/R/fwrite.R @@ -36,7 +36,6 @@ fwrite = function(x, file="", append=FALSE, quote="auto", nThread = as.integer(nThread) # write.csv default is 'double' so fwrite follows suit. write.table's default is 'escape' # validate arguments - rn = if (row.names) row.names(x) else NULL # allocate row.names in R to address integer row.names #4957 if (is.matrix(x)) { # coerce to data.table if input object is matrix messagef("x being coerced from class: matrix to data.table") x = as.data.table(x) @@ -112,7 +111,7 @@ fwrite = function(x, file="", append=FALSE, quote="auto", file = enc2native(file) # CfwriteR cannot handle UTF-8 if that is not the native encoding, see #3078. .Call(CfwriteR, x, file, sep, sep2, eol, na, dec, quote, qmethod=="escape", append, row.names, col.names, logical01, scipen, dateTimeAs, buffMB, nThread, - showProgress, is_gzip, bom, yaml, verbose, encoding, rn) + showProgress, is_gzip, bom, yaml, verbose, encoding) invisible() } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 7ba203f97..0ccda6f46 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -10706,26 +10706,30 @@ test(1733.2, fwrite(data.table(c(1.2,-8.0,pi,67.99),1:4),dec=",",sep=";"), # fwrite implied and actual row.names DT = data.table(foo=1:3,bar=c(1.2,9.8,-6.0)) -test(1734.1, capture.output(fwrite(DT,row.names=TRUE,quote=FALSE)), - capture.output(write.csv(DT,quote=FALSE))) -test(1734.2, capture.output(fwrite(DT,row.names=TRUE,quote=TRUE)), - capture.output(write.csv(DT))) -test(1734.3, fwrite(DT,row.names=TRUE,quote='auto'), # same other than 'foo' and 'bar' column names not quoted - output="\"\",foo,bar\n1,1,1.2\n2,2,9.8\n3,3,-6") +test(1734.01, capture.output(fwrite(DT,row.names=TRUE,quote=FALSE)), + capture.output(write.csv(DT,quote=FALSE))) +test(1734.02, capture.output(fwrite(DT,row.names=TRUE,quote=TRUE)), + capture.output(write.csv(DT))) +test(1734.03, fwrite(DT,row.names=TRUE,quote='auto'), # same other than 'foo' and 'bar' column names not quoted + output="\"\",foo,bar\n1,1,1.2\n2,2,9.8\n3,3,-6") DF = as.data.frame(DT) -test(1734.4, capture.output(fwrite(DF,row.names=TRUE,quote=FALSE)), - capture.output(write.csv(DF,quote=FALSE))) -test(1734.5, capture.output(fwrite(DF,row.names=TRUE,quote=TRUE)), - capture.output(write.csv(DF))) +test(1734.04, capture.output(fwrite(DF,row.names=TRUE,quote=FALSE)), + capture.output(write.csv(DF,quote=FALSE))) +test(1734.05, capture.output(fwrite(DF,row.names=TRUE,quote=TRUE)), + capture.output(write.csv(DF))) rownames(DF)[2] = "someName" rownames(DF)[3] = "another" -test(1734.6, capture.output(fwrite(DF,row.names=TRUE,quote=FALSE)), - capture.output(write.csv(DF,quote=FALSE))) -test(1734.7, capture.output(fwrite(DF,row.names=TRUE,quote=TRUE)), - capture.output(write.csv(DF))) -rownames(DF) = c(10L, 20L, 30L) ## test for 4957 -test(1734.8, capture.output(fwrite(DF, row.names = TRUE, quote = TRUE)), - capture.output(write.csv(DF))) +test(1734.06, capture.output(fwrite(DF,row.names=TRUE,quote=FALSE)), + capture.output(write.csv(DF,quote=FALSE))) +test(1734.07, capture.output(fwrite(DF,row.names=TRUE,quote=TRUE)), + capture.output(write.csv(DF))) +rownames(DF) = c(10L, -20L, 30L) ## test for #4957 +test(1734.08, capture.output(fwrite(DF, row.names=TRUE, quote=TRUE)), + capture.output(write.csv(DF))) +test(1734.09, capture.output(fwrite(DF, row.names=TRUE, quote=FALSE)), + capture.output(write.csv(DF, quote=FALSE))) +test(1734.10, fwrite(DF, row.names=TRUE, quote='auto'), + output=c('"",foo,bar','10,1,1.2','-20,2,9.8','30,3,-6')) # list columns and sep2 set.seed(1) diff --git a/src/fwrite.c b/src/fwrite.c index f7f400318..2d10d222f 100644 --- a/src/fwrite.c +++ b/src/fwrite.c @@ -623,8 +623,8 @@ void fwriteMain(fwriteMainArgs args) DTPRINT(_("... ")); for (int j=args.ncol-10; j