Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load .Rdata file into list()

Tags:

r

In R, load(file = "file.Rdata") will load all the variables as global variables. Is it possible to load all the variables contained in the .Rdata file into a list, so that it won't spoil the global variable space?

like image 920
Tomas Avatar asked Apr 23 '20 10:04

Tomas


2 Answers

You could assign it to a new environment and from there convert it into a list:

load("file.Rdata",  temp_env <- new.env())
env_list <- as.list(temp_env)
like image 193
Ritchie Sacramento Avatar answered Nov 19 '22 19:11

Ritchie Sacramento


Using load inside mget with other envir=onment than the .GlobalEnv.

d1 <- d2 <- d3  <- d4 <- data.frame()
save(d1, d2, d3, d4, file="test.rda")
rm(d1, d2, d3, d4)

x <- mget(load("test.rda", envir=(NE. <- new.env())), envir=NE.)
ls()
# [1] "NE." "x" 
x
# $d1
# data frame with 0 columns and 0 rows
# 
# $d2
# data frame with 0 columns and 0 rows
# 
# $d3
# data frame with 0 columns and 0 rows
# 
# $d4
# data frame with 0 columns and 0 rows
like image 42
jay.sf Avatar answered Nov 19 '22 19:11

jay.sf