Skip to content

Commit

Permalink
Added test of quote='auto' containing sep2 when and when not list col…
Browse files Browse the repository at this point in the history
…umns are present. Changed default sep2 from ; to | to distinguish it more from sep=, default. #1664
  • Loading branch information
mattdowle committed Nov 7, 2016
1 parent 4364b61 commit 0b656b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion R/fwrite.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fwrite <- function(x, file="", append=FALSE, quote="auto",
sep=",", sep2=c("",";",""), eol=if (.Platform$OS.type=="windows") "\r\n" else "\n",
sep=",", sep2=c("","|",""), eol=if (.Platform$OS.type=="windows") "\r\n" else "\n",
na="", dec=".", row.names=FALSE, col.names=TRUE,
qmethod=c("double","escape"),
logicalAsInt=FALSE, buffMB=8, nThread=getDTthreads(),
Expand Down
10 changes: 7 additions & 3 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -9566,8 +9566,8 @@ set.seed(1)
DT = data.table(A=1:4,
B=list(1:10,15:18,7,9:10),
C=list(letters[19:23],c(1.2,2.3,3.4,pi,-9),c("foo","bar"),c(TRUE,TRUE,FALSE)))
test(1736.1, capture.output(fwrite(DT)), c("A,B,C", "1,1;2;3;4;5;6;7;8;9;10,s;t;u;v;w",
"2,15;16;17;18,1.2;2.3;3.4;3.14159265358979;-9", "3,7,foo;bar", "4,9;10,TRUE;TRUE;FALSE"))
test(1736.1, capture.output(fwrite(DT)), c("A,B,C", "1,1|2|3|4|5|6|7|8|9|10,s|t|u|v|w",
"2,15|16|17|18,1.2|2.3|3.4|3.14159265358979|-9", "3,7,foo|bar", "4,9|10,TRUE|TRUE|FALSE"))
test(1736.2, fwrite(DT, sep2=","), error="length(sep2)")
test(1736.3, fwrite(DT, sep2=c("",",","")), error="sep.*,.*sep2.*,.*must all be different")
test(1736.4, fwrite(DT, sep2=c("","||","")), error="nchar.*sep2.*2")
Expand All @@ -9576,7 +9576,11 @@ test(1736.5, capture.output(fwrite(DT, sep='|', sep2=c("c(",",",")"))), c("A|B|C
test(1736.6, capture.output(fwrite(DT, sep='|', sep2=c("{",",","}"), logicalAsInt=TRUE)),
c("A|B|C", "1|{1,2,3,4,5,6,7,8,9,10}|{s,t,u,v,w}",
"2|{15,16,17,18}|{1.2,2.3,3.4,3.14159265358979,-9}", "3|{7}|{foo,bar}", "4|{9,10}|{1,1,0}"))

DT = data.table(A=c("foo","ba|r","baz"))
test(1736.7, capture.output(fwrite(DT)), c("A","foo","ba|r","baz")) # no list column so no need to quote
DT = data.table(A=c("foo","ba|r","baz"), B=list(1:3,1:4,c("fo|o","ba,r","baz"))) # now list column and need to quote
test(1736.8, capture.output(fwrite(DT)), c("A,B", "foo,1|2|3", "\"ba|r\",1|2|3|4", "baz,\"fo|o\"|\"ba,r\"|baz"))
test(1736.9, capture.output(fwrite(DT,quote=TRUE)), c("\"A\",\"B\"", "\"foo\",1|2|3", "\"ba|r\",1|2|3|4", "\"baz\",\"fo|o\"|\"ba,r\"|\"baz\""))


##########################
Expand Down
6 changes: 3 additions & 3 deletions man/fwrite.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
As \code{write.csv} and but \emph{much} faster (e.g. 1 minute versus 2 seconds) and just as flexible. Machines with 16GB of RAM or more always have more than one core so \code{fwrite} uses them; on all operating systems including Linux, Mac and Windows.
}
\usage{
fwrite(x, file = "", append = FALSE, quote = "auto", sep = ",", sep2 = c("",";",""),
fwrite(x, file = "", append = FALSE, quote = "auto", sep = ",", sep2 = c("","|",""),
eol = if (.Platform$OS.type=="windows") "\r\n" else "\n",
na = "", dec = ".", row.names = FALSE, col.names = TRUE, qmethod = c("double","escape"),
logicalAsInt = FALSE, buffMB = 8L, nThread = getDTthreads(),
Expand All @@ -17,9 +17,9 @@ fwrite(x, file = "", append = FALSE, quote = "auto", sep = ",", sep2 = c("",";",
\item{x}{A \code{data.table} or \code{data.frame} to write.}
\item{file}{Output file name. \code{""} indicates output to the console. }
\item{append}{If \code{TRUE}, the file is opened in append mode and column names (header row) are not written.}
\item{quote}{When \code{"auto"}, character columns, factor columns and column names will only be surrounded by double quotes if the field does contain the separator, a line ending or the quote itself. If \code{FALSE} the fields are never wrapped with quotes even if this would break the CSV due to the contents of the field. If \code{TRUE} quotes are always included for character and factor columns.}
\item{quote}{When \code{"auto"}, character fields, factor fields and column names will only be surrounded by double quotes when they need to be; i.e., the field contains the separator \code{sep}, a line ending \code{\\n}, the double quote itself (\code{\"}) or (when `list` columns are present) \code{sep2[2]}. If \code{FALSE} the fields are never wrapped with quotes even if this would break the CSV due to the contents of the field. If \code{TRUE} quotes are always included other than around numeric fields.}
\item{sep}{The separator between columns. Default is \code{","}.}
\item{sep2}{For columns of type \code{list} where each item is an atomic vector, \code{sep2} controls how to separate items \emph{within} the column. The 1st item is placed at the start of the output field, the 2nd item is placed between each item and the 3rd at the end. The 1st and 3rd item may be any length character strings including \code{""} (default) for nothing. The 2nd item must be a single character and different from `sep` and `dec`.}
\item{sep2}{For columns of type \code{list} where each item is an atomic vector, \code{sep2} controls how to separate items \emph{within} the column. \code{sep2[1]} is written at the start of the output field, \code{sep2[2]} is placed between each item and \code{sep2[3]} is written at the end. \code{sep2[1]} and \code{sep2[3]} may be any length character strings including \code{""} (default) for nothing. \code{sep2[2]} must be a single character (\code{nchar(sep2[2])==1)}) and different from `sep` and `dec`. This is only checked if \code{list} columns are present.}
\item{eol}{Line separator. Default is \code{"\r\n"} for windows and \code{"\n"} otherwise.}
\item{na}{The string to use for missing values in the data. Default is a blank string, \code{""}.}
\item{dec}{The decimal separator, by default \code{"."}. See link in references. Cannot be the same as \code{sep}.}
Expand Down

0 comments on commit 0b656b2

Please sign in to comment.