[R] load only one object from a .RData file

R. Michael Weylandt michael.weylandt at gmail.com
Tue Apr 24 21:31:46 CEST 2012

I don't think so -- you want to use saveRDS and readRDS if you want to do that.

However, if you are given someone else's .RData and need to avoid a
name collision, this might work.

loadOneName <- function(objName, file, envir = parent.frame(),
assign.on.exit = TRUE) {
    tempEnv <- new.env()
    load(file, envir = tempEnv)
    stopifnot(objName %in% ls(tempEnv))
    if(assign.on.exit) {
        assign(objName, tempEnv[[objName]], envir = envir)

Note that's not been thoroughly tested but it seems decent...


On Tue, Apr 24, 2012 at 2:30 PM, Shi, Tao <shidaxia at yahoo.com> wrote:
> Hi list,
> Is there a way to load one specific object from a .RData file which contains multiple data objects?  Thanks,
> ...Tao
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list