listing contents of an R data file without loading

后端 未结 4 689
南笙
南笙 2020-11-29 09:35

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 th

相关标签:
4条回答
  • 2020-11-29 09:42

    Maybe,

    load( "myDataFile.RData",ex<-new.env() )
    content=ls.str(ex)
    
    0 讨论(0)
  • 2020-11-29 09:43
    attach(file);ls(pos=2);detach(pos=2)
    

    That'll do it. Probably. #untested

    0 讨论(0)
  • 2020-11-29 09:49

    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.

    0 讨论(0)
  • 2020-11-29 10:04

    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.

    0 讨论(0)
提交回复
热议问题