Skip to content

Commit

Permalink
fwrite coerces input to data.table when given a matrix (#3125)
Browse files Browse the repository at this point in the history
  • Loading branch information
fparages authored and mattdowle committed Dec 6, 2018
1 parent f0023a1 commit 7eefead
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

4. `NA` in `between`'s `lower` and `upper` are now taken as missing bounds and return `TRUE` rather than than `NA`. This is now documented.

5. `fwrite()` now accepts `matrix`, (#2613)[https://github.com/Rdatatable/data.table/issues/2613]. Thanks to Michael Chirico for the suggestion and Felipe Parages for implementing. For now matrix input is converted to data.table (which can be costly) before writing.

#### BUG FIXES

1. Providing an `i` subset expression when attempting to delete a column correctly failed with helpful error, but when the column was missing too created a new column full of `NULL` values, [#3089](https://github.com/Rdatatable/data.table/issues/3089). Thanks to Michael Chirico for reporting.
Expand Down
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
message("x being coerced from class: matrix to data.table")
x <- as.data.table(x)
}
stopifnot(is.list(x),
identical(quote,"auto") || identical(quote,FALSE) || identical(quote,TRUE),
is.character(sep) && length(sep)==1L && nchar(sep) == 1L,
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -9213,6 +9213,12 @@ test(1658.32, fwrite(data.table(D = as.POSIXct(seq.Date(as.Date("2038-01-19"), a

options(oldverbose)

# input is of class matrix
test(1658.33, fwrite(matrix("foo"), quote=TRUE), output='"V1"\n"foo"', message = "x being coerced from class: matrix to data.table")
test(1658.34, fwrite(matrix(1:4, nrow=2, ncol=2), quote = TRUE), output = '"V1","V2"\n1,3\n2,4', message = "x being coerced from class: matrix to data.table")
test(1658.35, fwrite(matrix(1:3, nrow=3, ncol=1), quote = TRUE), output = '"V1"\n1\n2\n3', message = "x being coerced from class: matrix to data.table")
test(1658.36, fwrite(matrix(1:4, nrow=2, ncol=2, dimnames = list(c("ra","rb"),c("ca","cb"))), quote = TRUE), output = '"ca","cb"\n1,3\n2,4', message = "x being coerced from class: matrix to data.table")

## 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 @@ -20,7 +20,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 internally coerced to \code{data.table} preserving col names but not 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

0 comments on commit 7eefead

Please sign in to comment.