Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fwrite coerces input to data.table when given a matrix #3125

Merged
merged 9 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions R/fwrite.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ 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
if(is.matrix(x)){ # coerce to data.table if input object is matrix
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
warning("x coerced from class: matrix to data.table")
x <- as.data.table(x)
}
stopifnot(is.list(x), ncol(x) > 0L,
identical(quote,"auto") || identical(quote,FALSE) || identical(quote,TRUE),
is.character(sep) && length(sep)==1L && nchar(sep) == 1L,
Expand Down
3 changes: 3 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -9092,6 +9092,9 @@ test(1658.27, fwrite(ok_dt, col.names="foobar"), error="isLOGICAL(col.names)")
# null data table (no columns)
test(1658.28, fwrite(data.table(a=1)[NULL,]), error="ncol(x) > 0L is not TRUE")

# input is of class matrix
test(1658.29, fwrite(matrix("foo"), quote=TRUE), output='"V1"\n"foo"\n')

## End fwrite tests

# tests for #679, inrange(), FR #707
Expand Down
2 changes: 1 addition & 1 deletion man/fwrite.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fwrite(x, file = "", append = FALSE, quote = "auto",
verbose = getOption("datatable.verbose", FALSE))
}
\arguments{
\item{x}{Any \code{list} of same length vectors; e.g. \code{data.frame} and \code{data.table}.}
\item{x}{Any \code{list} of same length vectors; e.g. \code{data.frame} and \code{data.table}. If \code{matrix}, it gets coerced to \code{data.table} without preserving row names}
\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 fields, factor fields and column names will only be surrounded by double quotes when they need to be; i.e., when the field contains the separator \code{sep}, a line ending \code{\\n}, the double quote itself or (when \code{list} columns are present) \code{sep2[2]} (see \code{sep2} below). If \code{FALSE} the fields are not wrapped with quotes even if this would break the CSV due to the contents of the field. If \code{TRUE} double quotes are always included other than around numeric fields, as \code{write.csv}.}
Expand Down