[R] write.csv feature/bug with pipe
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Mar 25 11:33:19 CEST 2007
This is simply user error: the column names _are_ written to the pipe.
Because the connection was not open, the connection is opened to write the
column names, closed, opened to write the data and then closed.
In any case, you should call close() on connections you create to avoid
leaking connection structures. The following works:
con <- pipe("cat > d2.csv", "w")
write.csv(d, file=con)
close(con)
write.table() does exactly the same thing.
On Sun, 11 Mar 2007, ivo welch wrote:
> gentoo linux, version 2.4.1:
>
>> d= as.data.frame(matrix(1:20, 4, 5))
>> d
> V1 V2 V3 V4 V5
> 1 1 5 9 13 17
> 2 2 6 10 14 18
> 3 3 7 11 15 19
> 4 4 8 12 16 20
>> write.csv(d, file="d1.csv");
>> write.csv(d, file=pipe("cat > d2.csv"))
>> write.csv(d, file=pipe("gzip -c > d3.csv.gz"), col.names=T)
> Warning message:
> attempt to change 'col.names' ignored in: write.csv(d, file =
> pipe("gzip -c > d4.csv.gz"), col.names = T)
(Seems unlikely the message got the file name wrong here.)
> exit and
>
> $ head d1.csv
> "","V1","V2","V3","V4","V5"
> "1",1,5,9,13,17
> "2",2,6,10,14,18
> "3",3,7,11,15,19
> "4",4,8,12,16,20
> $ head d2.csv
> "1",1,5,9,13,17
> "2",2,6,10,14,18
> "3",3,7,11,15,19
> "4",4,8,12,16,20
>
> is it a bug or a feature that when pipe is used, the col.names is set
> to false? I guess I can invoke write.table to get the headers back.
>
> regards,
>
> /iaw
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list