Reputation: 37
How does one load an .RData
file that we know is a data frame such that one can use it as such? So I'm thinking along the lines of:
PATH <- getwd()
FILE_NAME <- "some_data_frame"
FILE_EXTENSION <- ".RData"
FILE_PATH <- paste0(PATH, "/", FILE_NAME, FILE_EXTENSION)
load(FILE_PATH)
So I did some more searching and I have an answer to my own question. This is what worked for me:
load(FILE_PATH)
df <- eval(parse(text = FILE_NAME))
It seems that the generic way to convert a string to an object is to use this eval-parse idiom.
Upvotes: 1
Views: 1520
Reputation: 161055
By default, load
returns a character
vector with the names of variables that have been restored into the current environment.
If you don't want to load everything from the .rda
(RData) file into the local environment, then I suggest a temporary environment:
e <- new.env(parent = emptyenv())
load(FILE_PATH, envir = e)
data_frame <- e[["name_of_your_frame"]]
In fact, everything that is in the .rda
file is now within the e
object. (There is no way to load only one of many objects in the .rds
file, it's all-or-nothing.)
Upvotes: 1