Skip to content

Commit 8039a23

Browse files
Relax fwrite dec == sep restriction for single-column input (#7233)
* added ncol condition * Use named error, use ncol * updated test * added rows condition * clean up tests * fine tune * use NROW/NCOL for more robust input checking --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 558084e commit 8039a23

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
106106
17. `t1 - t2`, where one is an `IDate` and the other is a `Date`, are now consistent with the case where both are `IDate` or both are `Date`, [#4749](https://github.com/Rdatatable/data.table/issues/4749). Thanks @George9000 for the report and @MichaelChirico for the fix.
107107
108+
18. `fwrite` now allows `dec` to be the same as `sep` for edge cases where only one will be written, e.g. 0-row or 1-column tables. [#7227](https://github.com/Rdatatable/data.table/issues/7227). Thanks @MichaelChirico for the report and @venom1204 for the fix.
109+
108110
### NOTES
109111
110112
1. The following in-progress deprecations have proceeded:

R/fwrite.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fwrite = function(x, file="", append=FALSE, quote="auto",
4444
is.character(sep) && length(sep)==1L && (nchar(sep) == 1L || identical(sep, "")),
4545
is.character(sep2) && length(sep2)==3L && nchar(sep2[2L])==1L,
4646
is.character(dec) && length(dec)==1L && nchar(dec) == 1L,
47-
dec != sep, # sep2!=dec and sep2!=sep checked at C level when we know if list columns are present
47+
`dec and sep must be distinct whenever both might be needed` = (!NROW(x) || NCOL(x) <= 1L || dec != sep), # sep2!=dec and sep2!=sep checked at C level when we know if list columns are present
4848
is.character(eol) && length(eol)==1L,
4949
length(qmethod) == 1L && qmethod %chin% c("double", "escape"),
5050
length(compress) == 1L && compress %chin% c("auto", "none", "gzip"),

inst/tests/tests.Rraw

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10977,9 +10977,9 @@ test(1732.7, fwrite(DT, quote='auto'), output='A,B\n,5\nNA,7\n"",0\nmonty,')
1097710977
test(1732.8, fwrite(DT, quote='auto', na="NA"), output='A,B\nNA,5\n"NA",7\n"",0\n"monty",NA')
1097810978

1097910979
# dec=","
10980-
test(1733.1, fwrite(data.table(pi),dec=","), error=base_messages$stopifnot("dec != sep"))
10980+
# Test 1733.1 removed, see #7227
1098110981
test(1733.2, fwrite(data.table(c(1.2,-8.0,pi,67.99),1:4),dec=",",sep=";"),
10982-
output="V1;V2\n1,2;1\n-8;2\n3,14159265358979;3\n67,99;4")
10982+
output="V1;V2\n1,2;1\n-8;2\n3,14159265358979;3\n67,99;4")
1098310983

1098410984
# fwrite implied and actual row.names
1098510985
DT = data.table(foo=1:3,bar=c(1.2,9.8,-6.0))
@@ -21593,3 +21593,12 @@ test(2336.3, all.equal(as.Date(t1) - t2, t1 - t2))
2159321593
test(2336.4, all.equal(as.Date(t2) - t1, t2 - t1))
2159421594
test(2336.5, all.equal(t1 - as.Date(t2), t1 - t2))
2159521595
test(2336.6, all.equal(t2 - as.Date(t1), t2 - t1))
21596+
21597+
# fwrite: allow dec=',' with single column, #7227
21598+
test(2337.1, fwrite(data.table(1), dec=","), NULL)
21599+
if (getRversion() >= "4.0.0") { # rely on stopifnot(named = ...) for correct message
21600+
test(2337.2, fwrite(data.table(0.1, 0.2), dec=",", sep=","), error = "dec and sep must be distinct")
21601+
}
21602+
test(2337.3, is.null(fwrite(data.table(c(0.1, 0.2)), dec=",", sep="\t")))
21603+
test(2337.4, is.null(fwrite(data.table(a=numeric(), b=numeric()), dec=",", sep=",")))
21604+
test(2337.5, is.null(fwrite(data.table(a=numeric()), dec=",", sep=",")))

0 commit comments

Comments
 (0)