23

I sometimes use print( load( "myDataFile.RData" ) ) to list the contents of a data file when I load it. Is there a way to list the contents without loading the objects contained in the data file?

JD Long
  • 59,675
  • 58
  • 202
  • 294

4 Answers4

6

I do not think you could do that without loading the object.

A solution could be to save the R objects with a wrapper to save, which function would save the object AND the structure of the object to a special Rdata file. Later you could load the special binary file with a wrapper to load, where you could specify to only list the structure of the data.

I have done something like this in a very basic package, named saves, can be found on CRAN.


Update: I made up a very simple metadata solution

save.ls <- function(x, file) {
    save(list=x, file=file)
    l <- ls()
    save(l, file=paste(file, 'ls', sep=''))
}
load.ls <- function(file) {
    attach(paste(file, 'ls', sep=''));
    return(l)
    detach(pos=2)
}

Save with save.ls instead of save and load with load.ls to test. Meta information is saved in separate file (ending in "ls"), but the mechanism could be improved easily e.g. making a tar archive (like I do in the package linked above) of the Rdata object and the file containing the metadata.

daroczig
  • 28,004
  • 7
  • 90
  • 124
  • A metadata solution does make sense. I don't know beans about R data files and thought they might have internal metadata – JD Long Jan 28 '11 at 22:30
  • @JD Long: that is the only way I could imagine. I made up a primitive example in my answer above to show what I meant. It could be implemented easily in a much neater way though, so this demo function is definitely not ready for production use :) – daroczig Feb 15 '11 at 14:35
  • 3
    The metadata solution in R exists already and is called called lazy-loading - see http://stackoverflow.com/questions/8700619/get-specific-object-from-rdata-file – Simon Urbanek Jan 02 '12 at 16:32
5
attach(file);ls(pos=2);detach(pos=2)

That'll do it. Probably. #untested

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • You can omit `pos=2` in `detach`, since `detach()` detaches 2nd item in search path by default. – aL3xa Jan 28 '11 at 17:54
  • 1
    I tested that suggestion by Ripley that I found in the archive and did not submit it, since it _did_ load a rather large dataset I used as my example. – IRTFM Jan 28 '11 at 18:13
  • 1
    attach and load are the only well-defined ways to access .RData files from the R level (you could open them as a binary connection and read if you knew the file format). If someone wants to write C code that goes beyond that (or R code on a binary connection), I'm sure it might be welcome into the core... – Spacedman Jan 28 '11 at 18:38
  • 2
    Yes, `attach()` *does* call `load()` is it makes no difference, you could as well simply use `local(load(file))` with the same result. – Simon Urbanek Jan 02 '12 at 16:31
3

In R v3.0.1 the load() function got a new argument. Loading an RData file with

load("mydata.RData", verbose=TRUE) 

will show you the objects that are loaded. Of course, it still means you have to load the object.

ph0t0nix
  • 535
  • 14
  • 25
1

Maybe,

load( "myDataFile.RData",ex<-new.env() )
content=ls.str(ex)
Marc S.
  • 13
  • 3