如何将对象加载到从R数据文件中指定的变量名中?

时间:2021-02-06 17:51:37

When you save a variable in an R data file using save, it is saved under whatever name it had in the session that saved it. When I later go to load it from another session, it is loaded with the same name, which the loading script cannot possibly know. This name could overwrite an existing variable of the same name in the loading session. Is there a way to safely load an object from a data file into a specified variable name without risk of clobbering existing variables?

当您使用save将变量保存到R数据文件中时,它将保存在保存它的会话中的任何名称下。当我稍后从另一个会话加载它时,它将加载相同的名称,加载脚本不可能知道这个名称。这个名称可以覆盖加载会话中相同名称的现有变量。是否有一种方法可以安全地将数据文件中的对象加载到指定的变量名中,而不会破坏现有的变量?

Example:

Saving session:

x = 5
save(x, file="x.Rda")

Loading session:

x = 7
load("x.Rda")
print(x) # This will print 5. Oops.

How I want it to work:

x = 7
y = load_object_from_file("x.Rda")
print(x) # should print 7
print(y) # should print 5

5 个解决方案

#1


71  

If you're just saving a single object, don't use an .Rdata file, use an .RDS file:

如果你只保存一个对象,不要使用。rdata文件,使用。rds文件:

x <- 5
saveRDS(x, "x.rds")
y <- readRDS("x.rds")
all.equal(x, y)

#2


28  

You can create a new environment, load the .rda file into that environment, and retrieve the object from there. However, this does impose some restrictions: either you know what the original name for your object is, or there is only one object saved in the file.

您可以创建一个新环境,将.rda文件加载到该环境中,并从该环境中检索对象。但是,这确实施加了一些限制:要么您知道对象的原始名称是什么,要么文件中只保存了一个对象。

This function returns an object loaded from a supplied .rda file. If there is more than one object in the file, an arbitrary one is returned.

此函数返回从提供的.rda文件中加载的对象。如果文件中有多个对象,则返回一个任意对象。

load_obj <- function(f)
{
    env <- new.env()
    nm <- load(f, env)[1]
    env[[nm]]
}

#3


19  

I use the following:

我使用下面的:

loadRData <- function(fileName){
#loads an RData file, and returns it
    load(fileName)
    get(ls()[ls() != "fileName"])
}
d <- loadRData("~/blah/ricardo.RData")

#4


18  

You could also try something like:

你也可以试试:

# Load the data, and store the name of the loaded object in x
x = load('data.Rsave')
# Get the object by its name
y = get(x)
# Remove the old object since you've stored it in y 
rm(x)

#5


1  

In case anyone is looking to do this with a plain source file, rather than a saved Rdata/RDS/Rda file, the solution is very similar to the one provided by @Hong Ooi

如果有人希望使用普通的源文件而不是保存的Rdata/RDS/Rda文件来实现这一点,那么解决方案与@Hong Ooi提供的解决方案非常相似

load_obj <- function(fileName) {

  local_env = new.env()
  source(file = fileName, local = local_env)

  return(local_env[[names(local_env)[1]]])

}

my_loaded_obj = load_obj(fileName = "TestSourceFile.R")

my_loaded_obj(7)

Prints:

打印:

[1] "Value of arg is 7"

[1]“arg值为7”

And in the separate source file TestSourceFile.R

在单独的源文件TestSourceFile.R中

myTestFunction = function(arg) {
  print(paste0("Value of arg is ", arg))
}

Again, this solution only works if there is exactly one file, if there are more, then it will just return one of them (probably the first, but that is not guaranteed).

同样,这个解决方案只在只有一个文件的情况下有效,如果有更多的文件,那么它将返回其中一个文件(可能是第一个文件,但这并不保证)。

#1


71  

If you're just saving a single object, don't use an .Rdata file, use an .RDS file:

如果你只保存一个对象,不要使用。rdata文件,使用。rds文件:

x <- 5
saveRDS(x, "x.rds")
y <- readRDS("x.rds")
all.equal(x, y)

#2


28  

You can create a new environment, load the .rda file into that environment, and retrieve the object from there. However, this does impose some restrictions: either you know what the original name for your object is, or there is only one object saved in the file.

您可以创建一个新环境,将.rda文件加载到该环境中,并从该环境中检索对象。但是,这确实施加了一些限制:要么您知道对象的原始名称是什么,要么文件中只保存了一个对象。

This function returns an object loaded from a supplied .rda file. If there is more than one object in the file, an arbitrary one is returned.

此函数返回从提供的.rda文件中加载的对象。如果文件中有多个对象,则返回一个任意对象。

load_obj <- function(f)
{
    env <- new.env()
    nm <- load(f, env)[1]
    env[[nm]]
}

#3


19  

I use the following:

我使用下面的:

loadRData <- function(fileName){
#loads an RData file, and returns it
    load(fileName)
    get(ls()[ls() != "fileName"])
}
d <- loadRData("~/blah/ricardo.RData")

#4


18  

You could also try something like:

你也可以试试:

# Load the data, and store the name of the loaded object in x
x = load('data.Rsave')
# Get the object by its name
y = get(x)
# Remove the old object since you've stored it in y 
rm(x)

#5


1  

In case anyone is looking to do this with a plain source file, rather than a saved Rdata/RDS/Rda file, the solution is very similar to the one provided by @Hong Ooi

如果有人希望使用普通的源文件而不是保存的Rdata/RDS/Rda文件来实现这一点,那么解决方案与@Hong Ooi提供的解决方案非常相似

load_obj <- function(fileName) {

  local_env = new.env()
  source(file = fileName, local = local_env)

  return(local_env[[names(local_env)[1]]])

}

my_loaded_obj = load_obj(fileName = "TestSourceFile.R")

my_loaded_obj(7)

Prints:

打印:

[1] "Value of arg is 7"

[1]“arg值为7”

And in the separate source file TestSourceFile.R

在单独的源文件TestSourceFile.R中

myTestFunction = function(arg) {
  print(paste0("Value of arg is ", arg))
}

Again, this solution only works if there is exactly one file, if there are more, then it will just return one of them (probably the first, but that is not guaranteed).

同样,这个解决方案只在只有一个文件的情况下有效,如果有更多的文件,那么它将返回其中一个文件(可能是第一个文件,但这并不保证)。