Use a list to 'transport' a collection of data sets and results

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Use a list to 'transport' a collection of data sets and results

christiaan pauw-2
Hi Everybody

I need to "transport" some data and results to use another application
(Sweave via LyX - where debugging is very difficult) in order to build a
report.

Is it possible to store a collection of variables of different types (like
named integers, matricies, data frames and two lists) all in one list and
save it and then simply load it again later and "unpack" the variables.

Thanks is advance
Christiaan

Please see the sample code below.

# Create variables of different types

x=expand.grid(1:4,letters[1:4])

y=matrix(1:1000,ncol=20)

z=list(a=1,b=2:20,c=matrix(1:50,ncol=5))

result.one=1

names(result.one)="first result"

result.two=2

names(result.one)="second result"


# Put them all in list

allvars=list(x,y,z,result.one,result.two)

names(allvars)=c("x","y","z","result.one","result.two")

ls()

[1] "allvars"    "result.one" "result.two" "x"          "y"          "z"

# Save it

save(allvars,file="allvars.Rda")
# Now remove it with rm(allvars) and load it again :

load("allvars.Rda")

What I need here is the magic function that will 'unpack" the list and give
the same results to ls()
ls()

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Use a list to 'transport' a collection of data sets and results

Dieter Menne
christiaan pauw-2 wrote
Hi Everybody

Is it possible to store a collection of variables of different types (like
named integers, matricies, data frames and two lists) all in one list and
save it and then simply load it again later and "unpack" the variables.
The easy way out is not to pack them from the beginning.

save(a,b,c,d,e,f, file=.)

I use this frequently to cache temporary results for Sweave (yes, I know there are automatic packages for that, but I prefer the manual way)

And loading can be done into an environment to avoid pollution.

Dieter

Reply | Threaded
Open this post in threaded view
|

Re: Use a list to 'transport' a collection of data sets and results

David Winsemius
In reply to this post by christiaan pauw-2

On Jul 27, 2011, at 6:27 AM, christiaan pauw wrote:

> Hi Everybody
>
> I need to "transport" some data and results to use another application
> (Sweave via LyX - where debugging is very difficult) in order to  
> build a
> report.
>
> Is it possible to store a collection of variables of different types  
> (like
> named integers, matricies, data frames and two lists) all in one  
> list and
> save it and then simply load it again later and "unpack" the  
> variables.

The usual method is to use the 'save' function and then restore or  
recover objects with the 'load' function.

You could I suppose add an additional (needless) layer of making a  
list , saving, loading,  and then "assign'-ing values to 'names' of  
that list.

--
David.

>
> Thanks is advance
> Christiaan
>
> Please see the sample code below.
>
> # Create variables of different types
>
> x=expand.grid(1:4,letters[1:4])
>
> y=matrix(1:1000,ncol=20)
>
> z=list(a=1,b=2:20,c=matrix(1:50,ncol=5))
>
> result.one=1
>
> names(result.one)="first result"
>
> result.two=2
>
> names(result.one)="second result"
>
>
> # Put them all in list
>
> allvars=list(x,y,z,result.one,result.two)
>
> names(allvars)=c("x","y","z","result.one","result.two")
>
> ls()
>
> [1] "allvars"    "result.one" "result.two" "x"          "y"          
> "z"
>
> # Save it
>
> save(allvars,file="allvars.Rda")
> # Now remove it with rm(allvars) and load it again :
>
> load("allvars.Rda")
>
> What I need here is the magic function that will 'unpack" the list  
> and give
> the same results to ls()
> ls()
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.