[R] write.csv to text string?
peter dalgaard
pdalgd at gmail.com
Thu Mar 12 21:50:27 CET 2015
Or, more in line with what was asked:
con <- textConnection("foo", "w")
write.csv(file=con, airquality)
close(con)
foo
It does, incidentally, look possible to equip write.table (of which write.csv is a special case) with an intern=TRUE setting, which could effectively do the above internally and return the result. There is no documented return value from write.table as far as I can tell.
-pd
> On 12 Mar 2015, at 19:25 , Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
> Hello,
>
> Maybe using text connections. See ?textConnection.
>
> tc <- textConnection("foo", "w")
> s <- 'cola,colb\n1,2\n2,3\n'
> cat(s, file = tc)
> close(tc)
> foo
>
> read.csv(text = foo)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 12-03-2015 17:15, Thomas Nyberg escreveu:
>> Hello,
>>
>> I've found the following useful functionality:
>>
>>> s <- 'cola,colb\n1,2\n2,3\n'
>>> read.csv(text=s)
>> cola colb
>> 1 1 2
>> 2 2 3
>>
>>
>> But I haven't found a similar option in write.csv. I.e. I would like to
>> "write" a dataframe to a string. What would be the easiest way to go
>> about such a thing? Right now I can only think of using a file as an
>> intermediary, but that seems a bit silly. Thanks for any help.
>>
>> Cheers,
>> Thomas Nyberg
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list