Search code examples
robjectrdata

Get specific object from Rdata file


I have a Rdata file containing various objects:

 New.Rdata
  |_ Object 1  (e.g. data.frame)
  |_ Object 2  (e.g. matrix)
  |_...
  |_ Object n

Of course I can load the data frame with load('New.Rdata'), however, is there a smart way to load only one specific object out of this file and discard the others?


Solution

  • .RData files don't have an index (the contents are serialized as one big pairlist). You could hack a way to go through the pairlist and assign only entries you like, but it's not easy since you can't do it at the R level.

    However, you can simply convert the .RData file into a lazy-load database which serializes each entry separately and creates an index. The nice thing is that the loading will be on-demand:

    # convert .RData -> .rdb/.rdx
    e = local({load("New.RData"); environment()})
    tools:::makeLazyLoadDB(e, "New")
    

    Loading the DB then only loads the index but not the contents. The contents are loaded as they are used:

    lazyLoad("New")
    ls()
    x # if you had x in the New.RData it will be fetched now from New.rdb
    

    Just like with load() you can specify an environment to load into so you don't need to pollute the global workspace etc.