Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examining contents of .rdata file by attaching into a new environment - possible?

Tags:

r

I am interested in listing objects in an RDATA file and loading only selected objects, rather than the whole set (in case some may be big or may already exist in the environment). I'm not quite clear on how to do this when there are conflicts in names, as attach() doesn't work as nicely.

1: For examining the contents of an R data file without loading it: This question is similar, but different from, the one asked at listing contents of an R data file without loading

In that case, the solution offered was:

attach(filename)
ls(pos = 2)
detach()

If there are naming conflicts between objects in the file and those in the global environment, this warning appears: The following object(s) are masked _by_ '.GlobalEnv':

I tried creating a new environment, but I cannot seem to attach into that. For instance, this produces the same error:

lsfile   <- function(filename){
  tmpEnv <- new.env()
  evalq(attach(filename), envir = tmpEnv)
  tmpls <- ls(pos = 2)
  detach()
  return(tmpls)
}
lsfile(filename)

Maybe I've made a mess of things with evalq (or eval). Is there some other way to avoid the naming conflict?

2: If I want to access an object - if there are no naming conflicts, I can just work with the one from the .rdat file, or copy it to a new one. If there are conflicts, how does one access the object in the file's namespace?

For instance, if my file is "sample.rdat", and the object is surveyData, and a surveyData object already exists in the global environment, then how can I access the one from the file:sample.rdat namespace?

I currently solve this problem by loading everything into a temporary environment, and then copy out what's needed, but this is inefficient.

like image 659
Iterator Avatar asked Jul 01 '11 16:07

Iterator


2 Answers

Since this question has just been referenced let's clarify two things:

  1. attach() simply calls load() so there is really no point in using it instead of load

  2. if you want selective access to prevent masking it's much easier to simply load the file into a new environment:

    e = local({load("foo.RData"); environment()})
    

    You can then use ls(e) and access contents like e$x. You can still use attach on the environment if you really want it on the search path.

FWIW .RData files have no index (the objects are stored in one big pairlist), so you can't list the contained objects without loading. If you want convenient access, convert it to the lazy-load format instead which simply adds an index so each object can be loaded separately (see Get specific object from Rdata file)

like image 186
Simon Urbanek Avatar answered Sep 19 '22 19:09

Simon Urbanek


I just use an env= argument to load():

> x <- 1; y <- 2; z <- "foo"
> save(x, y, z, file="/tmp/foo.RData")
> ne <- new.env()
> load(file="/tmp/foo.RData", env=ne)
> ls(env=ne)
[1] "x" "y" "z"
> ne$z
[1] "foo"
> 

The cost of this approach is that you do read the whole RData file---but on the other hand that seems to be unavoidable anyway as no other method seems to offer a list of the 'content' of such a file.

like image 45
Dirk Eddelbuettel Avatar answered Sep 20 '22 19:09

Dirk Eddelbuettel