[R] Storing data frame in a RDBMS

Adam Witney awitney at sgul.ac.uk
Mon Jun 6 11:29:43 CEST 2005


On 4/6/05 9:18 pm, "Joe Conway" <mail at joeconway.com> wrote:

> Gabor Grothendieck wrote:
>> On 6/4/05, Adam Witney <awitney at sgul.ac.uk> wrote:
>>> I am using PL/R in PostgreSQL amd have written some functions to build my
>>> data frame. However this can take some time with some large datasets and I
>>> would like to not have to repeat the process and so I would like to save the
>>> data frame. Rather than save/load into the file system I would like to be
>>> able to save the entire data frame as a single object in the database
>>> 
>>> Is this possible?
>> 
>> Check out ?serialize
> 
> Looks like serialize should work nicely:
> 
> create or replace function test_serialize(text)
> returns text as '
>  mydf <- pg.spi.exec(arg1)
>  return (serialize(mydf, NULL, ascii = TRUE))
> ' language 'plr';
> 
> create table saved_df (id int, df text);
> 
> insert into saved_df
> select 1, f from test_serialize('select oid, typname from pg_type
>                                  where typname = ''oid''
>                                  or typname = ''text''') as t(f);
> 
> create or replace function restore_df(text)
> returns setof record as '
>  unserialize(arg1)
> ' language 'plr';
> 
> select * from restore_df((select df from saved_df where id =1))
> as t(oid oid, typname name);
> oid | typname
> -----+---------
>  25 | text
>  26 | oid
> (2 rows)
> 
> HTH,

That looks great Joe, Gabor. Thanks for your help.

Cheers

Adam



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the R-help mailing list