[Rd] Subtle bug in write.csv

Hadley Wickham h.wickham at gmail.com
Thu Apr 4 15:49:49 CEST 2013

There's a subtle bug in write.csv -

write.csv(mtcars, "mtcars.csv", row = FALSE)
# Error in write.table(mtcars, "mtcars.csv", row = FALSE, col.names = NA,  :
#  col.names = NA makes no sense when row.names = FALSE

write.csv(mtcars, "mtcars.csv", row.names = FALSE)
# Works

i.e. the special evaluation used by write.csv has broken R's regular
argument matching mechanics.

This is straightforward to fix.  Replace the first line:

Call <- match.call(expand.dots = TRUE)


Call <- match.call(write.table, expand.dots = TRUE)

so the named arguments in write.table are used for argument matching.

Alternatively, I think you could replace the complex call munging just
by calling read.table with different default arguments:

write.csv <- function(x, file = "", sep = ",", qmethod = "double", ...) {
  write.table(x = x, file = file, sep = sep, qmethod = qmethod, ...)

but perhaps this introduces some undesirable behaviour that I'm missing.


Chief Scientist, RStudio

More information about the R-devel mailing list