从 Rdata 文件中获取特定对象 [英] Get specific object from Rdata file

查看:94
本文介绍了从 Rdata 文件中获取特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种对象的 Rdata 文件:

I have a Rdata file containing various objects:

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

当然,我可以使用 load('New.Rdata') 加载数据框,但是,是否有一种聪明的方法可以仅从该文件中加载一个特定对象并丢弃其他对象?

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?

推荐答案

.RData 文件没有索引(内容被序列化为一个大对列表).您可以破解一种方法来遍历配对列表并仅分配您喜欢的条目,但这并不容易,因为您无法在 R 级别执行此操作.

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

但是,您可以简单地将 .RData 文件转换为延迟加载数据库,该数据库单独序列化每个条目并创建索引.好处是可以按需加载:

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

就像 load() 一样,您可以指定要加载到的环境,这样您就不需要污染全局工作区等.

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

这篇关于从 Rdata 文件中获取特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆