[R] write.csv to text string?

Thomas Nyberg tomuxiong at gmail.com
Thu Mar 12 19:23:11 CET 2015


(Forgot to CC my response to the list...)

Thanks a lot this is exactly what I'm looking for! This is how I'll
probably use it...

----------------
> a
  cola colb
1    1    2
2    2    3

> f <- textConnection("s_a", "w")

> write.csv(a, f, row.names=F)

> close(f)

# Next do something with s_a...though probably not the following which #
is pointless.

> s_a
[1] "\"cola\",\"colb\"" "1,2"               "2,3"

> read.csv(text=s_a)
  cola colb
1    1    2
2    2    3
----------------

Thanks again everyone!

Cheers,
Thomas

On 03/12/2015 02:12 PM, John McKown wrote:
> On Thu, Mar 12, 2015 at 12:15 PM, Thomas Nyberg <tomuxiong at gmail.com> wrote:
>> 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
> 
> Perhaps something like:
> 
> textConn=textConnection("textCSV",open='w');
> write.csv(file=textConn,s)
> print(textCSV)
> 
> 
>



More information about the R-help mailing list