From h.wickham at gmail.com  Thu Apr  4 15:49:49 2013
From: h.wickham at gmail.com (Hadley Wickham)
Date: Thu, 4 Apr 2013 08:49:49 -0500
Subject: [Rd] Subtle bug in write.csv
Message-ID: <CABdHhvHsZDUm8BnsMS7TQ56Nc8WnqQgtLgfdoOeSR+szYTCR1w@mail.gmail.com>

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)

with

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.

Hadley

--
Chief Scientist, RStudio
http://had.co.nz/



[Rd] Subtle bug in write.csv
Hadley Wickham