[R] Importing a dataset

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Sep 25 14:46:54 CEST 2007


On Fri, 21 Sep 2007, Gabor Csardi wrote:

> I don't know a way of loading parts of an .RData file either,

You can't easily do it, as named objects are not stored separately in such 
a file (nor in memory in R).  See the 'R Internals' manual for a 
description of the format.  This would be possible, but you would have to 
read the whole .RData file to analyse its structure, then work out which 
SEXPRECs you need to reconstruct the desired object(s) and re-scan the 
file to bring those SEXPRECs into memory.

If we ever have a new save format, it would be worth bearing in mind that 
the desire to retrieve just some of the top-level saved objects comes up 
quite frequently, and perhaps store an index table (as e.g. PDF does).

> but another solution is to use the envir argument of load to
> load the data into a new environment:
>
>> x <- 1
>> y <- rnorm(3)
>> save.image("tmp.RData")
>> rm(x)
>> rm(y)
>> load("tmp.RData", env <- new.env())
>> get("x", env)
> [1] 1
>> get("y", env)
> [1] -0.1105102  0.6923334  1.5506114
>> rm(env)

This is essentially the same solution as Steve Ellison's, which uses the 
evaluation frame of get1() as the environment.  Using get(inherits=FALSE) 
would be a little safer (especially in Steve's version).

>
> Gabor
>
> On Fri, Sep 21, 2007 at 02:52:21PM +0100, S Ellison wrote:
>> I don't know a short way, but this worked when I tried it. Maybe there's a clue in there somewhere?
>>
>>  get1<-function(fname, varname) {
>>  load(fname)
>>  get(varname)
>>  }
>>
>> x<-1
>> y<-rnorm(3)
>>
>> save.image("temp.RData")
>>
>> rm(x)
>> rm(y)
>>
>> get1("temp.Rdata","x")
>>
>> get1("temp.Rdata","y")
>>
>>
>> Steve E
>>
>>>>> "Marco Venanzi" <marvena at tin.it> 17/09/2007 12:38:24 >>>

>> Hi,how can I load a dataset from another file R.Data,without importing 
>> all the objects (functions and other datasets) contained in that 
>> file?Thanks, Marco

-- 
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